代码之家  ›  专栏  ›  技术社区  ›  deft_code

一个类似于映射的容器,它将类型映射为值。

  •  7
  • deft_code  · 技术社区  · 15 年前

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

    我可以写这样一个容器,但我想找到一个已经写过/调试过的容器。我最大的障碍是没有好的关键词来搜索( 非均匀容器 不是我想要的。

    是否存在具有此接口或类似接口的Boost库?

    这个东西叫什么,这样我就可以更有效地搜索它了?


    更新:

    我不想找:

    • boost::mpl::map
      这将编译时值映射为编译时值
    • std::map<*,boost::any>
      这将静态类型运行时值映射为动态类型运行时值
    • std::map<*,boost::variadic<*>>
      这将静态类型的运行时值映射到变量类型的运行时值
    • std::map<typeid,boost::variadic<*>>
      这与我想要的很接近,只是它使用了rtti,如果使用错误的类型访问它,则不是编译错误。
    1 回复  |  直到 15 年前