嵌套模板要求使用template
关键字,如此answer中所述。这是一个简短的演示,它会导致编译器错误:
template <int T>
struct A {
template <int U>
static void b(int x) {
}
};
template <int T>
void test() {
A<T>::b<T>(T); // Should be:
// A<T>::template b<T>(T);
}
int main() {
test<0>();
}
作为一个挑战,我试图考虑一个程序,其中两种可能性(带有或不带有
template
)都是有效的,但是具有两种不同的含义,但是我无法全神贯注于运营商超载业务。是否可以使A<T>::b<T>(T);
是有效的语句? 最佳答案
如果您将struct A
更改为:
template <int T>
struct A {
static const int b = 5;
};
然后
A<T>::b<T>(T);
将进行编译。在这种情况下,
A<T>::b
为5。T
为0。因此5
0 > (0)
也是错误的;如果将代码替换为以下内容:
auto x = A<T>::b<T>(T);
std::cout << typeid(x).name() << " " << x;
你会得到:
布尔0
因此,修改
A
不太困难,以使A<T>::b<T>(T)
是有效的语句。关于c++ - 嵌套模板歧义,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58124891/