注册表中的不同类型的数据

use*_*456 5 c++ containers types stl

我想保留某种类型的容器,其中类型映射到该类型的一个值.基本上我想要的是一个std::map<std::typeindex, T>T取决于我用它索引的类型.std::map看起来不是一个好方法,因为类型是僵化的.我可以用来做这个的最简单的解决方案是什么?

Ker*_* SB 4

如果您映射到像 之类的类型擦除容器boost::any,那么您至少可以恢复该类型(如果您知道它是什么):

std::map<std::typeindex, boost::any> m;

m[typeid(Foo)] = Foo(1, true, 'x');

Foo & x = boost::any_cast<Foo&>(m[typeid(Foo)]);
Run Code Online (Sandbox Code Playgroud)

  • @user2852456:尝试用`void *`编写并比较! (3认同)