gpt4 book ai didi

c++ - 有哪些命名空间以及规则是什么?

转载 作者:行者123 更新时间:2023-12-01 12:20:39 25 4
gpt4 key购买 nike

注意:这个问题是关于name space ,不是 namespace .

C++ 标准对 name space 有一些引用。 ,但我没有看到这个的定义。标准说标签和宏位于不同的命名空间中。所有其他对 name space 的引用在 C/C++ 兼容性部分,像这样( current draft ):

This is one of the few incompatibilities between C and C++ that can be attributed to the new C++ name space definition where a name can be declared as a type and as a non-type in a single scope causing the non-type name to hide the type name and requiring that the keywords class, struct, union or enum be used to refer to the type name. This new name space definition provides important notational conveniences to C++ programmers and helps making the use of the user-defined types as similar as possible to the use of fundamental types.



这个新的命名空间定义是什么?我在哪里可以在标准中找到它?具体规则是什么?规则似乎比“非类型隐藏类型”更复杂。就像,这不会编译:
typedef int Foo; // Foo is a type
void Foo(); // not a type, but compile error, instead of hiding

但这确实:
struct Foo { }; // Foo is a type as well
void Foo(); // This hides the type Foo. "struct Foo" refers to the type

这也不能编译:
struct Foo { };   // Type
namespace Foo { } // Non-type, but compiler error instead of hiding

最佳答案

namespace 术语在 ISO C 标准中可能更为完善;引用 ISO C11 :

6.2.3 Name spaces of identifiers

If more than one declaration of a particular identifier is visible at any point in a translation unit, the syntactic context disambiguates uses that refer to different entities. Thus, there are separate name spaces for various categories of identifiers, as follows:

  • label names (disambiguated by the syntax of the label declaration and use);
  • the tags of structures, unions, and enumerations (disambiguated by following any32) of the keywords struct, union, or enum);
  • the members of structures or unions; each structure or union has a separate name space for its members (disambiguated by the type of the expression used to access the member via the . or -> operator);
  • all other identifiers, called ordinary identifiers (declared in ordinary declarators or as enumeration constants).

新品 然而,C++ 的 namespace 定义在任何时候都不是最近的,并且已经在 [diff.class]/1 中以其当前形式 ever since the ISO C++ Standard's introduction in '98 进行了描述。 .根据 OP 引用的 [diff.class]/1,它仅在与 ISO C 不同的上下文中以任何长度提及。
Afaics 我们需要求助于 ISO C11/6.2.3 并将其与 ISO C++ 标准的 [diff.class]/1 结合起来,以便对 C++ 的(新) namespace 定义进行连贯和完整的描述,而不是我们鞭打 ISO例如,C++ 标准 [basic.scope.hiding] , [class.name]/2 , [stmt.label]/1 , [cpp.replace]/8等等,看看它如何以及在哪里适用。

[class.name]/2

A class declaration introduces the class name into the scope where it is declared and hides any class, variable, function, or other declaration of that name in an enclosing scope. [...]

[stmt.label]/1

[...] Labels have their own name space and do not interfere with other identifiers [...]

[cpp.replace]/1

[...] There is one name space for macro names. [...]

关于c++ - 有哪些命名空间以及规则是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60722070/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com