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.

Kenny Peng
  • 1,891
  • 4
  • 18
  • 26
  • http://www.boost.org/doc/libs/1_43_0/libs/multi_index/doc/tutorial/key_extraction.html#member boost::multi_index_container allows this and that's how you specify the member on which you want to create an index on. – Kenny Peng Jul 26 '10 at 22:17
  • thanks, I looked it up, it specifies pointer to member variable. – Anycorn Jul 26 '10 at 23:24

3 Answers3

2

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 Is Pointer-to- " inner struct" member forbidden?

Community
  • 1
  • 1
AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
1

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
  • 26,501
  • 9
  • 66
  • 88
0

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