逗号 ( ,
) 是 std::initializer_list
中的序列点吗?
示例:这是否是 UB:
#include <vector>
int main()
{
auto nums = []
{
static unsigned x = 2;
return ( x++ % 2 ) + 1;
};
std::vector< int > v{ nums(), nums(), nums(), nums(), nums() };
// not sure if this is different: (note the additional brackets)
// std::vector< int > v({ nums(), nums(), nums(), nums(), nums() });
for( auto i : v )
{
std::cout << i;
}
return 0;
}
最佳答案
根据 C++11 § 8.5.4 [dcl.init.list] 第 4 段:
4 Within the initializer-list of a braced-init-list, the initializer-clauses, including any that result from pack expansions (14.5.3), are evaluated in the order in which they appear. That is, every value computation and side effect associated with a given initializer-clause is sequenced before every value computation and side effect associated with any initializer-clause that follows it in the comma-separated list of the initializer-list.
据我所知,GCC 4.8.1 有一个与初始值设定项评估相关的错误。我在这里描述了
http://cpp.forum24.ru/?1-3-0-00000063-000-0-0-1378892425
虽然文本是用俄语写的,但可以使用谷歌翻译等简单地将其翻译成英语。
关于c++ - std::initializer_list 和元素的求值顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20266153/