开发者

Referencing a member of a member in template

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
> >

H开发者_JAVA百科ow 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.


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?


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> >


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜