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

Is it allowed to typedef a class template type argument into the same name?

本文关键字:定义 类型参数 是否      更新时间:2023-10-16

这在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模板形参不能在其作用域中重新声明(包括嵌套作用域)。模板参数不能与模板名称。(例子:

<>之前模板<类T,>类Y {int T;//错误:template-parameter redeclarations无效f() {char T;//错误:template-parameter redeclarations}};template<类X>类X;//错误:template-parameter redeclarations之前

- end示例]

typedef EnumType EnumType是将模板参数重新定义为typedef-name