c++ - 是否允许将类模板类型参数定义为相同的名称?

标签 c++ templates typedef

这似乎在 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/

相关文章:

c++ - 在 C++ 中使用 setInfo() 会导致 E_ADS_INVALID_USER_OBJECT

c++ - 错误的模板参数数量错误

c - typedef 结构嵌套指针未定义错误

c - typedef 固定长度数组 -> 自动扩展大小?

c++ - 在 Windows 8 中从 mmc.exe 获取数字签名

c++ - 无法设置 gdb 断点

c++ - 从 Qt C++ 中动态创建的 Qline 编辑中获取文本值

templates - 一个应用程序中存在多个 Grails 脚手架模板

templates - 如何在 Yesod 中导入莎士比亚模板?

c - 请在这里解释 typedef 的行为