用户定义的文字,下划线和全局名称 [英] User-defined literals, underscores, and global names

查看:280
本文介绍了用户定义的文字,下划线和全局名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++ 11中,我们得到用户定义的文字。 C ++标准有这样的例子,例如:

In C++11, we get user-defined literals. The C++ standard has examples of these, such as:

long double operator "" _w(long double);

并且它说文字应以下划线开头:

And it says the literal should start with an underscore:


17.6.4.3.5用户定义的字面值

不以下划线开头的字面值标识符保留给未来

17.6.4.3.5 User-defined literal suffixes
Literal suffix identifiers that do not start with an underscore are reserved for future standardization.

但是,标准中还有另一部分说明

However, there's another section in the standard that says


17.6.4.3.2全局名称

某些名称和函数签名集始终保留给实现:

- 包含双下划线_ _或以下划线开头,后跟大写字母(2.12)的每个名称都保留给实现以供任何使用。

- 以下划线开头的每个名称保留给

17.6.4.3.2 Global names
Certain sets of names and function signatures are always reserved to the implementation:
— Each name that contains a double underscore _ _ or begins with an underscore followed by an uppercase letter (2.12) is reserved to the implementation for any use.
— Each name that begins with an underscore is reserved to the implementation for use as a name in the global namespace.

我希望更好地理解什么是17.6.4.3.2名称)说/意味着它与17.6.4.3.5(用户定义的字面意义)相关。具体来说:

I'm looking to better understand exactly what 17.6.4.3.2 (Global names) says/means and how it relates to 17.6.4.3.5 (User-defined literal suffixes). Specifically:


  • 17.6.4.3.2(全局名称)的第二部分需要用户定义的字面值$ c> _w )在命名空间中定义(即不在全局命名空间)?

  • 我假设17.6.4.3.2(全局名称)的第一部分排除用户定义的字面量,例如 _W (后接大写)和 __ w _w __ 下划线)。正确?

  • Does the second part of 17.6.4.3.2 (Global names) require user-defined literals (like the above _w) to be defined in a namespace (that is, not in the global namespace)? If so, I wish the standard would've illustrated this.
  • I presume that the first part of 17.6.4.3.2 (Global names) rules out user-defined literals like _W (followed by upper case) and __w and _w__ (two consecutive underscores). Correct?

编辑

作为后续,标准的一部分说:

As a follow up, there's a part of the standard that says:


13.5 .8用户定义的文本

[...]

2 声明标识符为literal-operator-id的声明应为声明命名空间范围函数或函数模板(可以是朋友函数(11.3)),显式实例化或函数模板的特化,或使用声明(7.3.3)。使用literal-operator-id声明的函数是文字运算符。以literal-operator-id声明的函数模板是一个文字操作符模板。

13.5.8 User-defined literals
[...]
2 A declaration whose declarator-id is a literal-operator-id shall be a declaration of a namespace-scope function or function template (it could be a friend function (11.3)), an explicit instantiation or specialization of a function template, or a using-declaration (7.3.3). A function declared with a literal-operator-id is a literal operator. A function template declared with a literal-operator-id is a literal operator template.

强调我。当它说namespace-scope这意味着用户定义的文字需要在用户定义的命名空间中声明(即不在全局命名空间)?

Emphasis mine. When it says "namespace-scope" does that mean user-defined literals need to be declared in a user-defined namespace (i.e. not in the global namespace)?

strong>稍后编辑:

Later edit:

问题首次提出时不存在,但现在还有

It did not exist when the question was first asked, but now there is also this related question and answer, which readers can additionally check after reviewing the answers below.

推荐答案

名称中有什么? 3基本概念[basic]告诉我们:

What's in a name? 3 Basic concepts [basic] tells us:


4名称是使用标识符(2.11),operator-function-id 13.5),literal-operator-id(13.5.8),conversion-function-id(12.3.2)或template-id(14.2)。

4 A name is a use of an identifier (2.11), operator-function-id (13.5), literal-operator-id (13.5.8), conversion-function-id (12.3.2), or template-id (14.2) that denotes an entity or label (6.6.4, 6.1).

我们与13.5.8用户定义的文字[over.literal]交叉引用:

which we cross-reference with 13.5.8 User-defined literals [over.literal]:


literal-operator-id

operator p>

literal-operator-id:
operator "" identifier

虽然文字运算符的名称涉及标识符,但该标识符不表示实体。 (或者它是一个不同的标识符和不同的名称,表示另一个实体或标签一起。)因此,文字运算符的名称从不以下划线开头。

While the name of a literal operator involves an identifier, that identifier does not denote an entity. (Or it's a different identifier and different name that denotes another entity or label altogether.) As such the name of a literal operator never starts with an underscore.

code> operator__ w 有问题,但这不是新的: int i__0;

Something like operator""__w is problematic but this is not new: int i__0; is reserved as well.

这篇关于用户定义的文字,下划线和全局名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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