编译器错误 C2942

“class”:type-class-id 重新定义为函数的形参

不能将泛型或模板类用作形参。 不能直接将参数传递到泛型或模板类的构造函数。

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

下面的示例生成 C2942:

// C2942.cpp
// compile with: /c
template<class T>
struct TC {};
void f(int TC<int>) {}   // C2942

// OK
struct TC2 {};
void f(TC2 i) {}

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

// C2942b.cpp
// compile with: /clr /c
generic<class T>
ref struct GC {};
void f(int GC<int>) {}   // C2942
ref struct GC2 { };
void f(int GC2) {}