小编Mat*_*hew的帖子

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

使用clang 3.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;
}
Run Code Online (Sandbox Code Playgroud)

但是,返回以下错误消息:

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 …
Run Code Online (Sandbox Code Playgroud)

c++ c++14

9
推荐指数
1
解决办法
380
查看次数

标签 统计

c++ ×1

c++14 ×1