用于增强多索引容器的模板化成员函数
因此我有一个增强多索引容器。
using namespace boost::multi_index;
template < typename O >
class Container
{
public:
multi_index_container<
O,
indexed_by<
ordered_unique<
const_mem_fun< O, std::string, &O::name >
>
>
> _container;
};
正如您所看到的,通过这种设计,我用来创建此容器的每个对象都必须有一个成员函数,返回一个名为“name”的字符串。
这显然并不理想。我尝试了几种传递“密钥”的方法,但我无法让它们中的任何一个起作用..
我尝试了这个..
using namespace boost::multi_index;
template < typename O, typename KT, typename KM >
class Container
{
public:
multi_index_container<
O,
indexed_by<
ordered_unique<
const_mem_fun< O, KT, &KM >
>
>
> _container;
};
int main( int c, char *v[] )
{
Container< Object, std::string, Object::name > container;
}
但没有喜悦..
编译器抱怨 Object::name 不是类型,而是我不知道如何纠正这个问题。即使我弄清楚如何向模板提供类型,我仍然需要容器使用“Object::name”的具体实例。
也许我必须提交类型,然后再输入构造时的成员函数?但是我该如何构建容器..我头疼!?!
下面的 Alexy 友善地提供了这个解决方案
using namespace boost::multi_index;
template < typename O, typename KT, KT (O::* KM)() >
class Container
{
public:
multi_index_container<
O,
indexed_by<
ordered_unique<
const_mem_fun< O, KT, KM >
>
>
> _container;
};
int main( int c, char *v[] )
{
Container< Object, std::string, &Object::name > container; // <<---- ERROR HERE
}
,但是,这会产生以下编译器错误。
Template parameter KM requires an expression of type std::string (Object::*)().
在标记的行..
好的。事实证明,这是我的错,因为提交了错误签名的“&Object::name”参数...我已经修复了这个..
I have a boost multi index container thus.
using namespace boost::multi_index;
template < typename O >
class Container
{
public:
multi_index_container<
O,
indexed_by<
ordered_unique<
const_mem_fun< O, std::string, &O::name >
>
>
> _container;
};
As you can see, by this design every object I use to create this container has to have a member function returning a string with the name "name".
This is obviously not ideal. I tried a couple of ways of passing in the "key" but I can't get any of them to work..
I tried this..
using namespace boost::multi_index;
template < typename O, typename KT, typename KM >
class Container
{
public:
multi_index_container<
O,
indexed_by<
ordered_unique<
const_mem_fun< O, KT, &KM >
>
>
> _container;
};
int main( int c, char *v[] )
{
Container< Object, std::string, Object::name > container;
}
but no joy..
the compiler complains that Object::name isn't a type but I'm not sure how to correct this. And even if I work out how to supply a type to the template, I'll still need a concrete instance of "Object::name" to be used by the container..
maybe I have to hand in the types and then and in the member function at construction? but then how do I construct the container .. My head hurts!?!
Alexy, below, kindly offered this solution
using namespace boost::multi_index;
template < typename O, typename KT, KT (O::* KM)() >
class Container
{
public:
multi_index_container<
O,
indexed_by<
ordered_unique<
const_mem_fun< O, KT, KM >
>
>
> _container;
};
int main( int c, char *v[] )
{
Container< Object, std::string, &Object::name > container; // <<---- ERROR HERE
}
However, this produded the following compiler error.
Template parameter KM requires an expression of type std::string (Object::*)().
at the line marked..
Ok. It turns out this was my fault by handing in an incorrectly signatured "&Object::name" parameter... I have fixed this..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更改类定义。
并使用
KM
而不是&KM
。Change class definition.
and use
KM
instead of&KM
.