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

def*_*ode 7 c++ templates metaprogramming

我正在寻找一个混合元容器/容器类.我想要一个将编译时类型映射到运行时值的类.代码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
}
Run Code Online (Sandbox Code Playgroud)

这真是无聊的东西.使用可变参数模板的实现会很有趣,但界面非常简单.

使这更难的部分是我想要的最后一个功能.

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.
}
Run Code Online (Sandbox Code Playgroud)

我可以编写这样一个容器,但我想找一个已经编写/调试过的容器.我最大的绊脚石是缺乏搜索的好关键词(异构容器不是我想要的).

是否有一个具有此接口或类似接口的Boost库?

这个东西叫什么,所以我可以更有效地谷歌?


更新:

我不是在寻找:

  • boost::mpl::map
    这会将编译时值映射到编译时值
  • std::map<*,boost::any>
    这会将静态类型运行时值映射到动态类型运行时值
  • std::map<*,boost::variadic<*>>
    这会将静态类型运行时值映射到变量类型运行时值
  • std::map<typeid,boost::variadic<*>>
    这接近我想要的,除了它使用RTTI,如果使用错误的类型访问它不是编译错误.