对函数参数的要求是否也适用于初始值设定项列表?
Do the Requirements Placed on Function Arguments Also Apply to Initializer Lists?
所以我在这里读到: https://stackoverflow.com/a/598150/2642059 这是非法的:
foo(i++, i++);
但我相信这是因为没有强制序列,据我所知,初始值设定项列表就是这种情况。那么这是法典吗?
const int foo[] = { i++, i++ };
是的,初始值设定项子句的求值顺序在大括号初始化列表中得到保证。
从标准 §11.6.4/4 列表初始化 [dcl.init.list]:
(强调我的)
在大括号初始化列表的初始值设定项列表中, 初始值设定项子句,包括由包扩展产生的任何子句, 按其显示顺序进行评估。也就是说,每个值 与给定初始值设定项子句相关的计算和副作用 在每个值计算和相关副作用之前进行排序使用逗号分隔的任何初始值设定项子句 初始值设定项列表的列表。[ 注意:此评估顺序成立 无论初始化的语义如何;例如,它 当初始值设定项列表的元素被解释为 构造函数调用的参数,即使通常没有 对调用参数的排序约束。— 尾注 ]
从 cppreference.com:
每个初始值设定项子句都按顺序排序 在任何之前 在大括号初始化列表中跟随它的初始值设定项子句。这是在 与函数调用的参数对比 表达 哪些是 未排序。
标准说明示例,
struct A { A(int, int) {} };
...
int i = 0;
A a1(i++, i++); // used as the arguments of the constructor; unsequenced
A a2{i++, i++}; // used as the arguments of the constructor; sequenced, within the initializer-list of a braced-init-list
相关文章:
- FLTK 2.0构建和演示,适用于VS2019的2011年左右的代码库
- C++17 - 使用自定义分配器的节点提取/重新插入 - 适用于 clang++/libc++,但不适用于 libstd
- "string.h"在构建适用于iOS的qt应用程序中找不到消息
- 适用于 WebView2 旧版本的示例应用程序
- 在 NVIDIA GEFORCE GTX 1050 上下载适用于 Windows 10 的 openCL 1.2
- 嵌套列表,用于在 C++ 中实现邻接列表
- __attribute__(优化(0))) 是否适用于"recursively"?
- 为什么 std::erase(std::erase_if) 不是适用于<algorithm>任何容器的模板?
- 使用一个参数的模板函数时出错(适用于 2)
- 使用 适用于 Android 和 iOS 的 tf-lite C++ API
- 为什么这适用于 G++ 而不是 CLANG?
- 适用于 macOS 的 Xcode 应用程序。这就是我设置从USB麦克风输入获取音频的方式。一年前工作,现在没有了。为什么
- 适用于 Linux 的 c++ 上的代理脚本
- 为什么我的 SFINAE 表达式不再适用于 GCC 8.2?
- 使输出流式处理运算符适用于 boost::variant<std::vector<int>、int、double 的正确方法是什么>
- 有没有适用于Windows.lib文件的GNU二进制文件描述符(BFD)
- 模板函数仅适用于VS
- 如何在cmake中包含适用于g++或viceversa的库
- 警告:扩展初始值设定项列表仅适用于-std=c++0x或-std=gnu++0x
- 警告:扩展初始化器列表仅适用于STD c++ 11