c++ - C++中的顶级是什么?

标签 c++ c++11 language-lawyer

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不是因为它嵌套在函数调用表达式中。

关于c++ - C++中的顶级是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64792031/

相关文章:

c# - 为什么 using 语句对 IEnumerator 起作用,它有什么作用?

python - "... object is not iterable"是 "x in y"的误导性错误吗?

c - 严格的别名规则指定不正确吗?

c++ - 指向结构体指针数组的指针

c++ - 在 C++ 中避免负值

linux - C++/Linux : Using c++11 atomic to avoid partial read on dual-mapped mmap region

c++ - 将 std::function 绑定(bind)到不同对象实例的相同函数

c++ - std::multiset 和上限和下限的奇怪行为

c++ - C++ std::numeric_limits<float>::max() 能否准确地存储在一个 float 中,然后进行比较?

std::multimap 中的 c++ 枚举类