gpt4 book ai didi

c++ - 使用 std::tie() 和赋值的评估顺序

转载 作者:行者123 更新时间:2023-12-03 18:52:03 26 4
gpt4 key购买 nike

以下是否有效?

template <typename T>
std::pair<T, T> foo(T one, T two) { ... }

std::tie(one, two) = foo(std::move(one), std::move(two));
(假设所涉及的类以有效的方式处理分配给移出的对象)。

从阅读更新 evaluation order proposal ,我的假设是这是固定的,但我无法在标准中找到一个确切的引用来验证这一点。有人可以帮忙提供吗?

最佳答案

标准中的相关部分可以在 [expr.ass]/1 中找到。它有

In all cases, the assignment is sequenced after the value computation of the right and left operands, and before the value computation of the assignment expression. The right operand is sequenced before the left operand. With respect to an indeterminately-sequenced function call, the operation of a compound assignment is a single evaluation.


所以,根据这个, foo(std::move(one), std::move(two));将首先评估,留下 onetwo从对象移动一次 std::tie(one, two)被评估。 tie创建引用,因此无法访问从那里移动的对象。然后分配实际上发生了意思 onetwo通过 std::tuple::operator = 分配给并获得任何值 foo返回。这是合法且明确的。

关于c++ - 使用 std::tie() 和赋值的评估顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66769512/

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