我正在寻找混合元容器/容器类。我想要一个将编译时类型映射到运行时值的类。一个代码片段相当于 1024 个单词,所以:
struct Foo { /* ... */ };
struct Bar { /* ... */ };
int main()
{
meta_container<Foo,float,int> mc;
mc.get<float>() = 1.0f;
mc.get<Foo>().method(blah);
mc.get<Bar>(); //compiler error
}
这真是无聊的事情。使用可变参数模板的实现会很有趣,但界面非常简单。
使这变得更加困难的部分是我想要的最后一个功能。
void foo( const meta_constainer<Foo,Bar,Baz>& mc );
//make_mc is sorta like make_pair or make_tuple.
int main()
{
foo( make_mc(Foo(), Bar(), Baz()) ); // not really interesting
foo( make_mc(Bar(), Foo(), Baz()) ); // this is more challenging
foo( make_mc(Foo()) ); // this might be difficult as well.
}
我可以编写这样的容器,但我想找到一个已经编写/调试过的容器。我最大的障碍是缺乏好的关键字来搜索(异构容器 不是我想要的)。
是否有具有此接口(interface)或类似接口(interface)的 Boost 库?
这个东西叫什么,所以我可以更有效地用谷歌搜索它?
更新:
我不是在寻找:
-
boost::mpl::map
这将编译时值映射到编译时值 -
std::map<*,boost::any>
这将静态类型运行时值映射到动态类型运行时值 -
std::map<*,boost::variadic<*>>
这将静态类型的运行时值映射到变量类型的运行时值 -
std::map<typeid,boost::variadic<*>>
这与我想要的很接近,除了它使用 RTTI 并且如果使用错误的类型访问它不是编译错误。
最佳答案
关于c++ - 一个类似 std::map 的容器,将类型映射到值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3980076/