Why are redundant scope qualifications supported by the compiler, and is it legal?(为什么编译器支持冗余范围限定,是否合法?)
问题描述
我在两个编译器上进行了测试,很惊讶地发现它们都支持以下定义而毫无怨言:
I tested on two compilers, and was surprised to see both support the following definition without complaint:
class A {
A();
};
A::A::A() {}
^^^
请注意,这也适用于方法,尽管在声明过度限定时会被标记.
Note that this also succeeds for methods, although it is flagged when the declaration is over-qualified.
问题:
- 这是一个有效的 C++ 程序吗?
- 如果是这样,它有什么用途 - 还是仅仅是副产品?
更新详情:
如果最初的问题不清楚或太短:我很好奇为什么在定义中允许使用多余的资格(重点也在上面添加).
In case the original question was not clear or too short: I'm curious why redundant qualifications are permitted on the definition (emphasis also added above).
Clang 和 Apple 的 GCC 4.2 + LLVM 是编译器
推荐答案
Yes, it's allowed (§9/2):
Yes, it's allowed (§9/2):
class-name 也插入到类本身的作用域中;这被称为注入类名.出于访问检查的目的,注入的类名被视为公共成员名.
The class-name is also inserted into the scope of the class itself; this is known as the injected-class-name. For purposes of access checking, the injected-class-name is treated as if it were a public member name.
有关导致类名注入的推理的信息,您可能需要阅读N0444.
For information about the reasoning that lead to class name inject, you might want to read N0444.
这篇关于为什么编译器支持冗余范围限定,是否合法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么编译器支持冗余范围限定,是否合法?
基础教程推荐
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- CString 到 char* 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 初始化列表*参数*评估顺序 2021-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
