[英]Template partial specialisation and dependent names
考虑以下
#include <iostream>
template <typename T, bool B>
struct C {
using value_type = T;
value_type f(value_type v);
};
template <bool B>
auto C<int, B>::f(value_type v) -> value_type {
return v;
}
int main(int argc, char * argv[]) {
C<int, true> c;
std::cout << c.f(5) << std::endl;
return 0;
}
使用g ++ 4.9我收到错误
test.cpp:11:26:错误:'C :: f'声明为'inline'变量
内联自动C :: f(value_type v) - > value_type {
test.cpp:11:26:错误:非模板'auto C :: f'的模板定义
test.cpp:11:26:错误:'value_type'未在此范围内声明
问题在于value_type
。 当我用typename C<int, B>::value_type
替换它时它会工作typename C<int, B>::value_type
但是这个更长,特别是在现实世界的应用程序中它可能很长(我的情况)。 有没有办法让这个工作与短变体?
PS:它适用于完整的模板专业化,但我必须只有部分专业化。
当您使用模板时,实际上您正在定义新类型,即使是模板专业化也是如此。
因此,正确的程序工作方式是完全重新定义专业化。
#include <iostream>
template <typename T, bool B>
struct C {
T f(T v);
};
template <bool B>
struct C<int,B> {
int f(int v) {
return v;
}
};
int main(int argc, char * argv[]) {
C<int, true> c;
std::cout << c.f(5) << std::endl;
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.