编译器错误 C2902

“token”:“template”后面出现意外标记,应为标识符

关键字 template 后面的标记不是标识符。

此错误在 Visual Studio 2022 及更高版本中已过时。

以下示例生成 C2902:

// C2902.cpp
// compile with: /c
namespace N {
   template<class T> class X {};
   class Y {};
}
void g() {
   N::template + 1;   // C2902
}

void f() {
   N::template X<int> x1;   // OK
}

使用泛型时也可能发生 C2902:

// C2902b.cpp
// compile with: /clr /c
namespace N {
   generic<class T> ref class GC {};
}

void f() {
   N::generic + 1;   // C2902
   N::generic GC<int>^ x;
}