Jam*_*lis 22
"未命名类型"实际上意味着"未命名的枚举或类类型"[有关更多信息,请参阅此答案的注释].枚举或类类型不必具有名称.例如:
struct { int i; } x; // x is of a type with no name
Run Code Online (Sandbox Code Playgroud)
您可以尝试通过参数推导使用未命名的类型作为模板参数:
template <typename T> void f(T) { }
struct { int i; } x;
f(x); // would call f<[unnamed-type]>() and is invalid in C++03
Run Code Online (Sandbox Code Playgroud)
请注意,此限制已在C++ 0x中解除,因此这将是有效的(您还可以使用本地类型作为类型模板参数).在C++ 0x中,您还可以使用decltype"命名"未命名的类型:
template <typename T> void g() { }
struct { int i; } x;
f<decltype(x)>(); // valid in C++0x (decltype doesn't exist in C++03)
Run Code Online (Sandbox Code Playgroud)