gpt4 book ai didi

c++ - 大括号初始化列表中的对象创建顺序

转载 作者:IT老高 更新时间:2023-10-28 22:25:16 34 4
gpt4 key购买 nike

#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 编译了它

最佳答案

在第二种情况下,您实际上只是使用 B(); 进行初始化;通过逗号操作符的使用,A()首先被构造和丢弃。

C {(A(), B())};
//|^^^^^^^^^^|
// \--------> (A(), B())
// ^^^ ^^^
// | |
// / \
// evaluated, THEN evaluated,
// discarded used

另一方面,在第一个实例中,您通过初始化列表从 both 临时变量中初始化 C,其元素也应该从左到右进行评估- 对,碰巧,但你的编译器在这方面有问题:

[C++11: 8.5.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. [ Note: This evaluation ordering holds regardless of the semantics of the initialization; for example, it applies when the elements of the initializer-list are interpreted as arguments of a constructor call, even though ordinarily there are no sequencing constraints on the arguments of a call. —end note ]

我可以使用 GCC 4.8* 重现该问题,但 Clang 3.5 的行为正常std-discussion 列表中已经讨论过该错误,但我还没有找到 GCC Bugzilla ID§。 p>

C {A(), B()};
// ^^^ ^^^
// | \
// eval- THEN
// uated evaluated
// \ /
// \ /
// both used

* http://coliru.stacked-crooked.com/a/1f18e0d1f8973f3c
http://coliru.stacked-crooked.com/a/5a6e7506e9be97c3
https://groups.google.com/a/isocpp.org/forum/#!topic/std-discussion/TQUnBFkUBDg
§ #51253可能是相关的。

关于c++ - 大括号初始化列表中的对象创建顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21234380/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com