用于增强多索引容器的模板化成员函数

发布于 2024-08-19 06:45:30 字数 1823 浏览 3 评论 0原文

因此我有一个增强多索引容器。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黒涩兲箜 2024-08-26 06:45:30

更改类定义。

template < typename O, typename KT, KT (O::* KM)() >
class Container 
//...

并使用 KM 而不是 &KM

Change class definition.

template < typename O, typename KT, KT (O::* KM)() >
class Container 
//...

and use KM instead of &KM.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文