简体   繁体   English

C++中的顶级是什么?

[英]What is top-level in C++?

In example 14 under [dcl.init.list] , the standard uses the term "top-level" when describing the semantics of code using list-initialization, in the context of narrowing conversions.[dcl.init.list]下的示例 14 中,在缩小转换的上下文中,在描述使用列表初始化的代码语义时,标准使用术语“顶级”。 I don't know what this means.我不知道这是什么意思。

This code executes without errors:此代码执行没有错误:

int f(int a) { return 1;}

int main() {
    int a[] = {f(2), f(2.0)};  
    int b[] = {f(2.0), f(2)}; // No error because: double-to-int conversion is not at the top-level
}

I also tried the following.我也尝试了以下方法。 I figured it is nothing to do with the order of initialization:我认为这与初始化顺序无关:

int f(int a) { return 1;}

int main() {
    int a[] = {f(2), f(2.0)};  
    int b[] = {f(2.0), f(2)}; // double-to-int conversion is not at the top-level
    //int c[] = {f(2147483645.0f), f(2)}; // This is erroring out due to narrowing.
    int d[] = {f(2), f(2.0)};  // Now I'm sure top-level doesn't mean the order of initialization. 
}

I want to know what is a top-level?我想知道什么是顶级? The documentations here and here do not describe it.这里这里的文档没有描述它。

The reason I'm curious about this term is because I'm trying to understand when would list-initializers work when narrowing conversion is implicitly invoked.我对这个术语感到好奇的原因是因为我试图了解当隐式调用缩小转换时列表初始化程序何时工作。

I'm also not sure about the terminology.我也不确定术语。 For example, is there something like a top-level class, or a top-level type, or a top-level list-initializer?例如,是否有顶级类、顶级类型或顶级列表初始化器之类的东西?

This is not a strictly defined term.这不是一个严格定义的术语。 But in [dcl.init.list]/note-7 that you linked, "at the top level" seems to mean "written directly in a braced list, rather than in a nested expression".但是在您链接的[dcl.init.list]/note-7中,“在顶层”似乎意味着“直接写在花括号列表中,而不是在嵌套表达式中”。

So, in int x[] = {1.0, f(2.0)};所以,在int x[] = {1.0, f(2.0)}; , 1.0 is at the top level, because it's written directly in the braced list, but 2.0 is not because it's nested in a function-call expression. , 1.0在顶层,因为它直接写在花括号列表中,但2.0不是因为它嵌套在函数调用表达式中。

This is not a rigorously defined C++ standard term.这不是一个严格定义的 C++ 标准术语。 Instead, it was just meant in the sloppy English language sense.相反,它只是在草率的英语语言意义上。 You can imagine someone using other phrases, like "earlier in the function" or "the corresponding header file", which are not technically C++ terms but would be widely understood.您可以想象有人使用其他短语,例如“函数中的早期”或“相应的头文件”,这些短语在技术上不是 C++ 术语,但会被广泛理解。

In this case, they're referring to a higer level of nesting.在这种情况下,他们指的是更高级别的嵌套。 For example, consider this snippet:例如,考虑这个片段:

void foo() {
    int j[2][2] = {{3, 4}, {5, 6}};
}

Clearly 4 is more deeply nested then {3, 4} , which in turn is more nested than {{3, 4}, {5, 6}} .显然4{3, 4}嵌套更深,而后者又比{{3, 4}, {5, 6}}嵌套更深。 In fact, {{3, 4}, {5, 6}} is not nested at all.实际上, {{3, 4}, {5, 6}}根本没有嵌套。 The author of the text you read would call this at the "top level".您阅读的文本的作者将其称为“顶级”。

Another author might argue that foo is on the "top level", so it depends on context to an extent.另一位作者可能会争辩说foo处于“顶级”,因此它在一定程度上取决于上下文。 In the quotation you posted, the meaning is clear though.在您发布的引文中,含义很清楚。

It is just an English phrase meant to be taken in context.它只是一个用于上下文的英语短语。

The rules of [dcl.init.list] tell us how an item in a list-initialiser cannot involve a narrowing conversion. [dcl.init.list]的规则告诉我们列表初始化程序中的项目如何不能涉及缩小转换。 Note 7 goes on to remind us of this, using the term "top-level" to describe those things that it's talking about (the immediate "members" of the list). Note 7 继续提醒我们这一点,使用术语“顶级”来描述它正在谈论的那些事情(列表的直接“成员”)。

Example 14, which you are referring to, further includes an example of where a statement with a list-initialisation happens to contain a narrowing conversion, but the comment reminds us that this is fine because the conversion occurs at a "lower level" of nesting, again using the term "top-level" to describe the entities listed immediately in the initialiser list.您所指的示例 14 还包括一个示例,说明带有列表初始化的语句恰好包含缩小转换,但注释提醒我们这很好,因为转换发生在嵌套的“较低级别” ,再次使用术语“顶级”来描述在初始化列表中立即列出的实体。 No narrowing conversion was required to be applied to the actual list item.不需要对实际列表项应用缩小转换。

There is no formal definition of the term "top-level"; “顶级”一词没有正式的定义; it's supposed to be interpreted in its English sense while considering the possible nesting at play.考虑到可能的嵌套,它应该按照英语的意思来解释。

I was confused about this a while back, but I think I understand this concept fairly well now.不久前我对此感到困惑,但我认为我现在对这个概念相当了解。

Before I go to explain the concept, it must be noted that it is not the object that is top-level or low-level, but it is the "const" that is top-level or low-level depending it's position in the expression or context.在我开始解释这个概念之前,必须注意,不是顶级或低级的对象,而是顶级或低级的“const”,这取决于它在表达式中的位置或上下文。

Top-level const means (that) "const" is making the object itself const (no matter what that object is pointing to).顶级const意味着(即)“const”使对象本身成为const(无论该对象指向什么)。

Low-level const means (that) "const" is indicating that the object being referred(/pointed) to is const.低级const 意味着(即)“const”表示被引用(/指向)的对象是 const。

Ex.前任。

const int i = 10; // const is indicating that i itself is const so it (const) is top level.

const int const *p = &i; // leftmost const is indicating that "pointed to" is const, so that const is low-level, other const in the expr is top-level.

Also, Top-level consts can appear with any obj type (Build in types, class type, pointer types), whereas Low-level const appears in base type of the compound types such as pointers & refs.此外,顶级 const 可以出现在任何 obj 类型(内置类型、类类型、指针类型)中,而低级 const 出现在复合类型(如指针和引用)的基类型中。

In pointers, both low-level and top-level can appear, but in refs, consts are always low-level, because refs are implicitly have top-level consts (once initialised, refs can't be bound to other objs, so in a sense they implicitly have top-level const on to them).在指针中,低级和顶级都可以出现,但在 refs 中,consts 总是低级的,因为 refs 隐式具有顶级 consts(一旦初始化,refs 就不能绑定到其他 obj,所以在从某种意义上说,它们隐含地拥有顶级常量)。

Hope that helps.希望有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM