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

Mat*_*hew 9 c++ c++14

使用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 argument used here
1>          Class_Breaks<int> break_error;
1>          ~~~~~~~~~~~~~~~~^
1>  1 error generated.
Run Code Online (Sandbox Code Playgroud)

Dan*_*rey 4

正如@StenSoft 所提到的,这是一个已知的错误。如果您需要使其工作,因为您constexpr想将模板变量用作默认值,则可以将默认值包装到std::intergral_constant

template<
    typename T,
    bool = std::integral_constant< bool, IS_SCALAR<T> >::value
>
Run Code Online (Sandbox Code Playgroud)

实例