是否允许将类模板类型参数定义为同名? [英] Is it allowed to typedef a class template type argument into the same name?

查看:31
本文介绍了是否允许将类模板类型参数定义为同名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎可以在 MSVC 中编译甚至按预期工作.但它是合法的 C++ 代码吗?它是否能保证执行此处预期的操作(即,将模板类型导出到同名下的结构体用户)?

template 结构体枚举{//CS中有两个难题:缓存失效和命名事物.typedef EnumType EnumType;};

解决方案

我认为类型定义是不允许的.

14.6.1 本地声明的名称 (N4296)

<块引用>

6 模板参数不应在其范围内重新声明(包括嵌套作用域).模板参数不得与模板名称.[示例:

<前>模板Y类{国际T;//错误:模板参数重新声明无效 f() {图表;//错误:模板参数重新声明}};模板X级;//错误:模板参数重新声明

——结束示例 ]

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

This seems to compile and even work as expected in MSVC. But is it legal C++ code and is it guaranteed to do what is expected here (that is, export the template type to the struct's users under the same name)?

template <typename EnumType>
struct Enum
{
   // There are two hard problems in CS: cache invalidation and naming things.
   typedef EnumType EnumType;
};

解决方案

I think the type definition is not allowed.

14.6.1 Locally declared names (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 ]

The typedef EnumType EnumType is a redefinition of the template-parameter as typedef-name.

这篇关于是否允许将类模板类型参数定义为同名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆