这似乎在 MSVC 中按预期编译甚至工作。但它是合法的 C++ 代码吗?它是否保证执行此处预期的操作(即,以相同的名称将模板类型导出给结构的用户)?
template <typename EnumType>
struct Enum
{
// There are two hard problems in CS: cache invalidation and naming things.
typedef EnumType EnumType;
};
最佳答案
我认为类型定义是不允许的。
14.6.1 本地声明的名称(N4296)
6 A template-parameter shall not be redeclared within its scope (including nested scopes).A template-parameter shall not have the same name as the template name. [ Example:
template<class T, int i> class Y { int T; // error: template-parameter redeclared void f() { char T; // error: template-parameter redeclared } }; template<class X> class X; // error: template-parameter redeclared— end example ]
typedef EnumType EnumType
是将模板参数重新定义为 typedef-name。
关于c++ - 是否允许将类模板类型参数定义为相同的名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38063271/