woo*_*ock 5 c++ compiler-errors template-specialization visual-c++ c++17
以下代码使用 C++17 在 Clang 15.0.7 和 GCC 12.2.0 编译器上成功编译:
template<typename T, std::size_t s = 0u>
struct A: std::false_type {};
template<typename T>
struct A<T, sizeof(T)>: std::true_type {};
static_assert(A<int, sizeof(int)>::value);
Run Code Online (Sandbox Code Playgroud)
但 MSVC 19.35.32215 失败并显示:
error C2753: 'A<T,sizeof(T)>': partial specialization cannot match argument list for primary template
error C2607: static assertion failed
Run Code Online (Sandbox Code Playgroud)
我已经有一个解决方法:
template<typename T, typename S>
struct B: std::false_type {};
template<typename T>
struct B<T, std::integral_constant<std::size_t, sizeof(T)>>: std::true_type {};
template<typename T, std::size_t s = 0u>
using A = B<T, std::integral_constant<std::size_t, s>>;
static_assert(A<int, sizeof(int)>::value);
Run Code Online (Sandbox Code Playgroud)
我知道“非类型模板参数不能专门化其类型取决于专门化参数的模板参数”,但在第一个代码示例中,类型始终且不std::size_t依赖于参数。只有非类型模板实参的值取决于参数。
根据标准,这是允许的吗?这是另一个 MSVC 错误吗?