【发布时间】:2015-12-15 15:15:55
【问题描述】:
我希望我的模板类不允许将非标量类型的数组作为模板参数,为此我编写了这些辅助类型:
template<bool> struct AssertTrue;
template<> struct AssertTrue<true> {};
template < class T>
struct FilterOutArraysWithNonScalarTypes
{
typedef std::true_type Allowed;
};
template < class T, size_t N>
struct FilterOutArraysWithNonScalarTypes<T[N]>
{
typedef std::integral_constant<bool, std::is_scalar<T>::value> Allowed;
};
然后在我的对象的构造函数中我检查这种方式
CheckAllowance<FilterOutArraysWithNonScalarTypes<T>::Allowed::value>;
我可以做得更好吗?
编辑:
抱歉,我用 CheckAllowance 错误打印了 AssertTrue。
【问题讨论】:
-
尝试使用 Boost MPL 库。它有一堆预定义的类和模板。
-
@randomusername 我不能