C++ boost::任何具有有限类型支持的
我希望有一个类似于C++ boost::任何具有有限类型支持的,c++,c++11,types,dynamic-typing,boost-any,C++,C++11,Types,Dynamic Typing,Boost Any,我希望有一个类似于boost::any的类型,但是有一个更有限的类型集。大概是这样的: limited_any<int,long,string> x; // x is like boost::any but is guaranteed to contain only an int, a long, or a string limited_any x;//x类似于boost::any,但保证只包含int、long或字符串 您建议如何实现这一点?(我自己或使用现有的解决方案)您正在寻
boost::any
的类型,但是有一个更有限的类型集。大概是这样的:
limited_any<int,long,string> x; // x is like boost::any but is guaranteed to contain only an int, a long, or a string
limited_any x;//x类似于boost::any,但保证只包含int、long或字符串
您建议如何实现这一点?(我自己或使用现有的解决方案)您正在寻找。您可以了解
boost::variant
是如何做到的。虽然boost::any
是与C的void*
相当的类型安全,但是boost::variant
更像C的union
@tohava注意到有一些区别。最大的实现差异是any
基于免费存储,而variant
基于自动存储。(另外,variant
比任何都有更丰富的类型安全接口)