tags:

views:

63

answers:

3

I'm using boost::multi_index_container and I'm trying to refer to a member of a member in a template argument, but it's unclear how to do this:

struct Foo {
    int unique_value;
};

struct Bar {
    Foo foo;
    double non_unique_value;
};

// I want to refer to some_value in a template argument:
multi_index_container<Bar, boost::multi_index::indexed_by<
    ordered_unique< member< Foo, int, &Bar::foo::unique_value > >, // this doesn't work
    ordered_non_unique< member< Bar, double, &Bar::non_unique_value > > // this works
> >

How can I refer to unique_value in the template argument? I understand why what I did doesn't work: I should be conveying that Foo is a type that is a member of Bar and be doing something more akin to Bar::Foo::some_value, but it's unclear how I can specify this.

+2  A: 

Questions about this feature pop-up from time to time, since it is indeed a very logical thing to have. But unfortunately it is not part of the language.

See this thread as well http://stackoverflow.com/questions/1929887/is-pointer-to-inner-struct-member-forbidden

AndreyT
+1  A: 

You could work around this with a suitable method in Bar

struct Bar {
    Foo foo;
    double non_unique_value;
    int get_unique_value() const { return foo.unique_value; }
};

and then use const_mem_fun

ordered_non_unique<  const_mem_fun<Bar,int,&Bar::get_unique_value> >
Aaron McDaid
Yeah, I ended up using `global_fun`.
Kenny Peng
A: 

You can write a user-defined key extractor that does the work.

Joaquín M López Muñoz