Link to a member of an element in a template

I use boost::multi_index_container, and I'm trying to refer to the member member in the template argument, but it is 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_valuein a template argument? I understand why what I did does not work: I must report that it Foois a type that is a member Barand do something more similar to Bar::Foo::some_value, but it is not clear how I can indicate this.

+3
source share
3 answers

Questions about this feature appear from time to time, as it is really very logical. But, unfortunately, this is not part of the language.

See also this topic. Is a pointer-to- “internal structure” member prohibited?

+2

Bar

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

const_mem_fun

ordered_non_unique<  const_mem_fun<Bar,int,&Bar::get_unique_value> >
+1

You can write a custom key extractor that does the job.

0
source

Source: https://habr.com/ru/post/1756646/


All Articles