Boost检查该类型是否属于给定类型的列表
boost check if the type belong to a list of given types
我有以下问题。在模板中,我想检查类型是否是给定类型之一。
代码描述:
tempalte <typename T>
class foo
{
public:
//BOOST_STATIC_ASSERT(T is one of int, long, long long, double ....);
//boost::is_scalar doesn't fill my requirements since I need
//to provide my own list of types
};
我知道如何使用模板规范进行操作,但是这种方式很乏味。
template <typename T>
class ValidateType
{
static const bool valid = false;
};
template <>
class ValidateType<int>
{
static const bool valid = true;
}
//repeat for every wanted type
有优雅的方式吗?
这有效:
#include <boost/mpl/set.hpp>
#include <boost/mpl/assert.hpp>
typedef boost::mpl::set<int, long, long long, double, ...> allowed_types;
BOOST_MPL_ASSERT((boost::mpl::has_key<allowed_types, int>)); // Compiles
BOOST_MPL_ASSERT((boost::mpl::has_key<allowed_types, char>)); // Causes compile-time error
您可以使用contains
算法使用MPL vector
(这是 type 的向量):
typedef vector<int, long, long long, double> types;
BOOST_MPL_ASSERT(( contains<types, T> ));
相关文章:
- 使用简单类型列表实现的指数编译时间.为什么
- 从类型列表中递归删除重复项会导致编译器堆空间错误 (VS2017)
- 测试两个类型列表中的所有组合
- 谷歌测试:模板模板的笛卡尔乘积的类型列表与模板
- 在迭代模板类型列表时无法停止递归
- 将可变参数类型列表的扩展打包为复杂类型的初始值设定项列表 - 合法吗?
- 阅读从 istream 到矢量的不同类型列表
- 如何在C++中创建类型列表的 n 路笛卡尔积?
- c++17通过生成预先声明的类型列表的笛卡尔乘积来生成std::变体
- 将变量模板限制为类型列表
- 我可以使用特征指定变体的类型列表吗?
- 如何对要用于模板参数的类型列表进行编码
- 创建类型列表并访问每种类型的静态成员?
- 为什么BOOST :: HANA :: EXAREMIS ::键入实验功能?类型列表的棘手是什么
- 如果创建支持返回可变参数类型列表的通用模板 API,我应该使用 std::tuple 还是其他东西?
- std::仅移动类型列表:不能在 VC++ 中放入 std::vector
- C++类型列表创建子列表
- 调用STD ::功能,并带有变体类型列表
- 在没有宏的情况下在 C++98 中创建类型列表时遇到问题
- 如何从类型列表重建参数包