Boost logo

Boost Users :

From: Filip Konvička (filip.konvicka_at_[hidden])
Date: 2007-03-15 05:45:12


> #define DEFINE_BIND_TYPE(name,expr) \
> typedef boost::remove_reference< \
> boost::remove_cv<typeof(expr)>::type>::type name##_expr_type; \
> \
> struct name:name##_expr_type \
> { \
> name():name##_expr_type(expr){} \
> };
>
> DEFINE_BIND_TYPE(
> extractor,
> boost::bind(&std::string::size,boost::bind(&foo::s,_1)))
>
> typedef multi_index_container<
> foo,
> indexed_by<
> hashed_unique<extractor>
> >
> > mi_t;
>
> Whether this is more convenient than writing your own key extractors is
> a debatable issue... One final warning: this trick relies on an undocumented
> feature of boost::bind, namely that the function objects produced by this
> lib own a result_type nested typedef, as required by the key extractor
> concept --otherwise the classes defined by DEFINE_BIND_TYPE
> wouldn't be conformant key extractors. So, if you want to stand on the
> documented side you couldn't use this. Also, this is one reason why
> thes idiom does not extend to boost::lambda expressions.
>
I've been playing with the problem too, and found this solution...it
does not rely on typeof, but it does rely on return_type. I'm a bit
unsure about why I had to use "unsigned int" to make it work, so I'd
welcome some comments on this.

template<class T, typename MemberType, MemberType T::* MemberPtr,
typename RetType, RetType (MemberType::*MemberFct)() const>
struct extract_and_call {
    typedef unsigned int result_type;
    unsigned int operator()(T const& t) const {
        return ((t .* MemberPtr) .* MemberFct)();
    }
};

typedef multi_index_container<
    foo,
    indexed_by<
        hashed_unique<
            extract_and_call<foo, std::string, &foo::s,
std::string::size_type, &std::string::size>
>,
        ordered_unique<
            extract_and_call<foo, std::string, &foo::s,
std::string::size_type, &std::string::size>
>
>
> mi_t;

Cheers,
Filip


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net