iav*_*avr 26 c++ templates partial-specialization non-type
也许我累了,但我坚持这个简单的部分专业化,这不起作用,因为non-type template argument specializes a template parameter with dependent type 'T':
template <typename T, T N> struct X;
template <typename T> struct X <T, 0>;
Run Code Online (Sandbox Code Playgroud)
更换0的T(0),T{0}或(T)0没有帮助.那么这种专业化甚至可能吗?
Con*_*tor 25
见标准的段落[temp.class.spec] 14.5.5/8:
与专用非类型参数对应的模板参数的类型不应取决于特化的参数.[ 例如:
Run Code Online (Sandbox Code Playgroud)template <class T, T t> struct C {}; template <class T> struct C<T, 1>; // error template< int X, int (*array_ptr)[X] > class A {}; int array[5]; template< int X > class A<X,&array> { }; // error- 末端的例子 ]
编辑的答案:最简单的解决方法是使用类型1替换非类型模板参数:
#include <type_traits>
template <typename T, typename U>
struct X_;
template <typename T, T N>
struct X_<T, std::integral_constant<T, N>> {};
template <typename T>
struct X_<T, std::integral_constant<T, 0>> {};
template <typename T, T N>
struct X : X_<T, std::integral_constant<T, N>> {};
Run Code Online (Sandbox Code Playgroud)
使用Yakk解决方案的解决方案:
#include <iostream>
#include <type_traits>
template <typename T, T N, typename = void >
struct X {
static const bool isZero = false;
};
template <typename T, T N>
struct X < T, N, typename std::enable_if<N == 0>::type > {
static const bool isZero = true;
};
int main(int argc, char* argv[]) {
std::cout << X <int, 0>::isZero << std::endl;
std::cout << X <int, 1>::isZero << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7823 次 |
| 最近记录: |