Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 是否可以使用`constexpr`模板变量作为正式模板参数的默认值_C++_C++14 - Fatal编程技术网

C++ 是否可以使用`constexpr`模板变量作为正式模板参数的默认值

C++ 是否可以使用`constexpr`模板变量作为正式模板参数的默认值,c++,c++14,C++,C++14,使用Clang3.6.0,我无法编译以下代码示例 #include <type_traits> template <typename T> constexpr bool IS_SCALAR = ::std::is_scalar<T>::value; template <typename T, bool = IS_SCALAR<T>> struct Class_Breaks { }; template <typename T,

使用Clang3.6.0,我无法编译以下代码示例

#include <type_traits>

template <typename T> constexpr bool IS_SCALAR = ::std::is_scalar<T>::value;
template <typename T, bool = IS_SCALAR<T>>
struct Class_Breaks
{
};

template <typename T, bool = ::std::is_scalar<T>::value>
struct Class_Works
{
};

void function()
{
    Class_Breaks<int> break_error;
    Class_Breaks<int, IS_SCALAR<int>> breaks_ok;
    Class_Works<int> ok;
}
#包括
模板constexpr bool IS_SCALAR=::std::IS_SCALAR::value;
模板
结构类中断
{
};
模板
结构类工程
{
};
空函数()
{
类中断错误;
等级(坏)(坏)(好);;
Class_工作正常;
}
但是,将返回以下错误消息:

1>  [ 66%] Building CXX object CMakeFiles/Core.dir/tests.cpp.obj
1>D:\Projects\Core\Core\tests.cpp(4,30): error : non-type template argument is not a constant expression
1>  template <typename T, bool = IS_SCALAR<T>>
1>                               ^
1>  D:\Projects\Core\Core\tests.cpp(16,18) :  note: while checking a default template argument used here
1>          Class_Breaks<int> break_error;
1>          ~~~~~~~~~~~~~~~~^
1>  1 error generated.
1>[66%]构建CXX对象cmakfiles/Core.dir/tests.cpp.obj
1> D:\Projects\Core\Core\tests.cpp(4,30):错误:非类型模板参数不是常量表达式
1> 模板
1>                               ^
1> D:\Projects\Core\Core\tests.cpp(16,18):注意:检查此处使用的默认模板参数时
1> 类中断错误;
1>          ~~~~~~~~~~~~~~~~^
1> 生成1个错误。

正如@StenSoft所提到的,它是一个。如果您需要使其工作,因为您有一个
constexpr
模板变量要用作默认值,您可以将默认值包装成
std::integral\u常量

template<
    typename T,
    bool = std::integral_constant< bool, IS_SCALAR<T> >::value
>
模板<
类型名T,
bool=std::积分常数::值
>

这在clang 3.7中没有固定。Daniel Frey提到的bug报告与constexpr函数(现在可以工作)有关,但与变量模板无关。

这是一个错误吗