简体   繁体   English

一个std :: map like容器,它将类型映射到值

[英]A std::map like container that maps types to values

I'm looking for a hybrid meta-container/container class. 我正在寻找一个混合元容器/容器类。 I want a class that maps a compile-time type to a runtime value. 我想要一个将编译时类型映射到运行时值的类。 A code snippit is worth 1024 words so: 代码snippit价值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
}

This is really boring stuff. 这真是无聊的东西。 An implementation using variadic templates would be interesting enough but the interface is very simple. 使用可变参数模板的实现会很有趣,但界面非常简单。

The part that makes this more difficult is this last feature I want. 使这更难的部分是我想要的最后一个功能。

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

I could write such a container, but I'd like to find one that's already written/debugged. 我可以编写这样一个容器,但我想找一个已经编写/调试过的容器。 My biggest stumbling block has been lack of good keywords to search for ( heterogeneous container is not what I want). 我最大的绊脚石是缺乏搜索的好关键词( 异构容器不是我想要的)。

Is there a Boost library that has this or a similar interface? 是否有一个具有此接口或类似接口的Boost库?

What is this thing called, so I can google it more effectively? 这个东西叫什么,所以我可以更有效地谷歌?


update: 更新:

I am not looking for: 我不是在寻找:

  • boost::mpl::map
    This maps a compile-time value to a compile-time value 这会将编译时值映射到编译时值
  • std::map<*,boost::any>
    This maps a static type runtime value to a dynamic type runtime value 这会将静态类型运行时值映射到动态类型运行时值
  • std::map<*,boost::variadic<*>>
    This maps a static typed runtime value to a variable type runtime value 这会将静态类型运行时值映射到变量类型运行时值
  • std::map<typeid,boost::variadic<*>>
    This is close to what I want except that it uses RTTI and it is not a compile error if accessed with the wrong type. 这接近我想要的,除了它使用RTTI,如果使用错误的类型访问它不是编译错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM