什么是C ++中的未命名类型? [英] What is an unnamed type in C++?

查看:647
本文介绍了什么是C ++中的未命名类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为我的厕所的一部分,在C ++标准ANSI ISO IEC 14882 2003,我发现了以下:

As part of my toilet reading on the C++ Standard ANSI ISO IEC 14882 2003, I came across the following:


14.3.1.2 :本地类型,没有链接的类型,未命名类型或类型
从任何这些类型复合
不能用作
模板的模板参数
type-parameter。

14.3.1.2: A local type, a type with no linkage, an unnamed type or a type compounded from any of these types shall not be used as a template-argument for a template type-parameter.

当我得到一个本地类型和一个复合类型,什么是未命名类型?如果一个类型未命名,你怎么可能试图在模板中使用它,这促使标准口头排除它?

While I get what a local type and a compound type are, what is an unnamed type? If a type is unnamed, how could you even attempt to use it in a template anyway, which prompted the standard to verbally exclude it?

推荐答案

未命名类型真的意味着未命名的枚举或类类型[有关更多信息,请参阅对此回答的评论]。枚举或类类型不必具有名称。例如:

"Unnamed type" really means "unnamed enumeration or class type" [for more information, see the comments to this answer]. An enumeration or class type doesn't have to have a name. For example:

struct { int i; } x; // x is of a type with no name

您可以尝试使用未命名的类型作为模板argument through argument deduction:

You could try to use an unnamed type as a template argument through argument deduction:

template <typename T> void f(T) { }

struct { int i; } x;
f(x); // would call f<[unnamed-type]>() and is invalid in C++03

请注意,此限制已在C ++ 0x中解除,因此此将有效(您也可以使用本地类型作为类型模板参数)。在C ++ 0x中,您还可以使用 decltype 来命名未命名的类型:

Note that this restriction has been lifted in C++0x, so this will be valid (you'll also be able to use local types as type template parameters). In C++0x, you could also use decltype to "name" an unnamed type:

template <typename T> void g() { }

struct { int i; } x;
f<decltype(x)>(); // valid in C++0x (decltype doesn't exist in C++03)

这篇关于什么是C ++中的未命名类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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