example 14 under [dcl.init.list] 中,在缩小转换的范围内,当使用列表初始化描述代码的语义时,标准使用术语“顶级”。我不知道这是什么意思。
这段代码执行没有错误:

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
}
我还尝试了以下方法。我认为这与初始化顺序无关:
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.
}
我想知道什么是顶层?文档herehere没有描述它。
我对该术语感到好奇的原因是,我试图了解隐式调用缩小转换时列表初始化器何时起作用。
我也不确定术语。例如,是否存在诸如顶级类,顶级类型或顶级列表初始化器之类的东西?

最佳答案

这不是严格定义的术语。但是在您链接的 [dcl.init.list]/note-7 中,“在顶层”似乎意味着“直接在括号列表中编写,而不是在嵌套表达式中编写”。
因此,在int x[] = {1.0, f(2.0)};中,1.0在顶层,因为它直接写在括号列表中,但是2.0并不是因为它嵌套在函数调用表达式中。

10-08 04:09