C++ 检查数组是否为标量类型

C++ 检查数组是否为标量类型,c++,arrays,templates,C++,Arrays,Templates,我希望我的模板类不允许带有非标量类型的数组作为模板参数,为此我编写了以下辅助类型: template<bool> struct AssertTrue; template<> struct AssertTrue<true> {}; template < class T> struct FilterOutArraysWithNonScalarTypes { typedef std::true_type Allowed; }; templa

我希望我的模板类不允许带有非标量类型的数组作为模板参数,为此我编写了以下辅助类型:

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;
};
template struct AssertTrue;
模板结构AssertTrue{};
模板
具有非标度类型的结构筛选器OutArray
{
typedef std::允许为true_类型;
};
模板
具有非标度类型的结构筛选器OutArray
{
typedef std::允许的积分_常数;
};
然后在我的对象的构造函数中,我这样检查

CheckAllowance<FilterOutArraysWithNonScalarTypes<T>::Allowed::value>;
支票津贴;
我能做得更好吗

编辑:


抱歉,我打印错了带支票津贴的AssertTrue。

您的
AssertTrue
未在您显示的代码中使用。我想您可以将其替换为
static\u assert()
。否则一切看起来都很好。

您可以通过一个
静态断言来实现这一点:

template <typename T>
struct Foo {
    static_assert(!(std::is_array<T>::value && 
                    !std::is_scalar<std::remove_extent_t<T>>::value),
                  "Must not be a non-scalar array");
};
模板
结构Foo{
静态_断言(!(std::is_数组::value&&
!std::is_scalar::value),
“不能是非标量数组”);
};
如果您觉得这太冗长,可以制作一个别名模板:

template <typename T>
using is_non_scalar_array = std::integral_constant<
                              bool,
                              std::is_array<T>::value && 
                              !std::is_scalar<std::remove_extent_t<T>>::value
                            >;
模板
使用is非标量数组=std::integral常量<
布尔,
std::is_数组::值&&
!std::是_scalar::value
>;
或作为变量模板:

template <typename T>
constexpr bool is_non_scalar_array = std::is_array<T>::value && 
                                     !std::is_scalar<std::remove_extent_t<T>>::value;
模板
constexpr bool is_non_scalar_array=std::is_array::value&&
!std::是_scalar::value;

尝试使用Boost MPL库。它有一堆预定义的类和模板,我不能