#include <iostream>
using namespace std;
template<typename T>
void test() {
cout << "1";
}
template<>
void test<std::string>() {
cout << "2";
}
int main() {
test<std::string()>(); //expected output 2 but actual output 1
}
Run Code Online (Sandbox Code Playgroud)
为什么输出1而不是2?
template<class T> struct A {
typedef int Int;
A::Int b; // Line 1 (fails)
Int c; // Line 2 (compiles)
};
int main(){
A<int> x;
x.c = 13;
}
Run Code Online (Sandbox Code Playgroud)
错误
error: ISO C++ forbids declaration of ‘Int’ with no type
error: extra qualification ‘A<T>::’ on member ‘Int’
error: expected ‘;’ before ‘b’
Run Code Online (Sandbox Code Playgroud)
第1行失败但第2行编译.为什么?