【发布时间】:2014-02-09 15:29:06
【问题描述】:
#include <iostream>
struct A
{
A() { std::cout << "(A::A)"; }
};
struct B
{
B() { std::cout << "(B::B)"; }
};
struct C
{
template<typename ...Args>
C(Args && ...) {}
};
int main(int agrc, char *argv[])
{
C {A(), B()}; // <-- prints (B::B)(A::A)
std::cout << std::endl;
C {(A(), B())}; // <-- prints (A::A)(B::B)
std::cout << std::endl;
return 0;
}
我有 2 个问题:
- 为什么在第一个大括号初始化列表中,对象是按从右到左的顺序创建的?
- 为什么第二种情况下的括号会恢复此顺序?
编辑:我已经用 msvs 2013 编译了它
【问题讨论】:
-
明显重现问题。
-
@dyp
C {A(), B()}; // <-- prints (B::B)(A::A)与你刚才所说的相矛盾吗? -
我认为第一行的输出显示了一个错误 - 根据 8.5.4/4,它应该从左到右进行评估。
-
@Borgleader 我想我搞错了:/
-
@dyp。我不这么认为。
In list-initialization, every value computation and side effect of 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.
标签: c++ visual-studio c++11 visual-studio-2013