C++17 引入的评估顺序保证是什么?

What are the evaluation order guarantees introduced by C++17?(C++17 引入的评估顺序保证是什么?)
本文介绍了C++17 引入的评估顺序保证是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C++17 评估顺序保证 (P0145) 中的投票对典型 C++ 代码有何影响?

它对以下内容有何改变?

i = 1;f(i++, i)

std::cout <<f() <

f(g(), h(), j());

解决方案

到目前为止,评估顺序未指定的一些常见情况在 C++17.一些未定义的行为现在是未指定的.

<块引用>

i = 1;f(i++, i)

未定义,但现在未指定.具体来说,没有指定的是 f 的每个参数相对于其他参数的评估顺序.i++ 可能在 i 之前计算,反之亦然.事实上,它可能会以不同的顺序评估第二次调用,尽管在同一个编译器下.

但是,在执行任何其他参数之前,需要对每个参数进行评估,以完全执行所有副作用.因此,您可能会得到 f(1, 1) (首先评估第二个参数)或 f(1, 2) (首先评估第一个参数).但是你永远不会得到 f(2, 2) 或任何其他类似的东西.

<块引用>

std::cout <<f() <

未指定,但它将与运算符优先级兼容,因此 f 的第一次评估将在流中首先出现(示例如下).

<块引用>

f(g(), h(), j());

仍有未指定的 g、h 和 j 评估顺序.请注意,对于 getf()(g(),h(),j()),规则规定 getf() 将在 g 之前计算,h, j.

还请注意提案文本中的以下示例:

<块引用>

 std::string s = 但我听说它有效,即使你不相信它"s.replace(0, 4, "").replace(s.find("even"), 4, "only").replace(s.find(不要"), 6, ");

该示例来自 The C++ Programming Language,第 4 版,Stroustrup,并且曾经是未指定的行为,但在 C++17 中它将按预期工作.可恢复函数 (.then( . . . )) 也存在类似问题.

作为另一个示例,请考虑以下内容:

#include <iostream>#include <字符串>#include <向量>#include <cassert>结构扬声器{诠释我=0;扬声器(std::vector words):words(words){}标准::向量<标准::字符串>字;标准::字符串运算符()(){断言(words.size()>0);if(i==words.size()) i=0;//C++17 之前的版本:auto word = words[i] + (i+1==words.size()?"
":",");++i;返回词;//使用 C++17 仍然不可能://return words[i++] + (i==words.size()?"
":",");}};int main() {auto spk = Speaker{{All", Work", and", no", play"}};std::cout <<spk() <<spk() <<spk() <<spk() <<spk() ;}

在 C++14 之前,我们可能(并且将会)得到如下结果

播放不,并且,工作,所有,

而不是

All,work,and,no,play

请注意,上面的效果与

((((((std::cout)<<spk())<<spk())<<spk())<<spk())<< spk()) ;

但是,在 C++17 之前,不能保证第一个调用会首先进入流.

参考:来自接受的提案:

<块引用>

后缀表达式从左到右计算.这包括函数调用和成员选择表达式.

赋值表达式从右到左计算.这包括复合作业.

移位运算符的操作数从左到右计算.在总结,以下表达式按 a 的顺序求值,然后b,然后是 c,然后是 d:

  1. a.b
  2. a->b
  3. a->*b
  4. a(b1, b2, b3)
  5. b @= a
  6. a[b]
  7. 一个<<b
  8. 一个>>b

此外,我们建议以下附加规则:涉及重载运算符的表达式的评估是由与相应内置相关联的顺序确定运算符,而不是函数调用的规则.

编辑说明:我的原始答案误解了 a(b1, b2, b3).b1b2b3 的顺序仍未指定.(谢谢@KABoissonneault,所有评论者.)

但是,(正如@Yakk 指出的那样)这很重要:即使 b1b2b3 是不平凡的表达式,在开始评估其他参数之前,它们中的每一个都被完全评估并绑定到各自的函数参数.标准是这样规定的:

<块引用>

§5.2.2 - 函数调用 5.2.2.4:

...后缀表达式在每个表达式之前排序表达式列表和任何默认参数.每个值计算和与参数初始化相关的副作用,以及初始化本身,在每个值计算之前排序,并且与任何后续初始化相关的副作用参数.

但是,GitHub 中缺少其中一个新句子草稿:

<块引用>

与参数的初始化以及初始化本身是在每个值计算和相关的副作用之前排序任何后续参数的初始化.

示例那里.它解决了几十年前的问题(正如 Herb Sutter 所解释的那样)具有异常安全性,例如

f(std::unique_ptra, std::unique_ptrb);f(get_raw_a(),get_raw_a());

如果其中一个调用 get_raw_a() 会在另一个调用之前抛出,则会泄漏原始指针与其智能指针参数绑定.

正如 T.C. 所指出的,该示例存在缺陷,因为从原始指针构造 unique_ptr 是显式的,因此无法编译.*

还要注意这个经典的问题(标记为C,而不是 C++):

<块引用>

int x=0;x++ + ++x;

仍未定义.

What are the implications of the voted in C++17 evaluation order guarantees (P0145) on typical C++ code?

What does it change about things like the following?

i = 1;
f(i++, i)

and

std::cout << f() << f() << f();

or

f(g(), h(), j());

解决方案

Some common cases where the evaluation order has so far been unspecified, are specified and valid with C++17. Some undefined behaviour is now instead unspecified.

i = 1;
f(i++, i)

was undefined, but it is now unspecified. Specifically, what is not specified is the order in which each argument to f is evaluated relative to the others. i++ might be evaluated before i, or vice-versa. Indeed, it might evaluate a second call in a different order, despite being under the same compiler.

However, the evaluation of each argument is required to execute completely, with all side-effects, before the execution of any other argument. So you might get f(1, 1) (second argument evaluated first) or f(1, 2) (first argument evaluated first). But you will never get f(2, 2) or anything else of that nature.

std::cout << f() << f() << f();

was unspecified, but it will become compatible with operator precedence so that the first evaluation of f will come first in the stream (examples below).

f(g(), h(), j());

still has unspecified evaluation order of g, h, and j. Note that for getf()(g(),h(),j()), the rules state that getf() will be evaluated before g, h, j.

Also note the following example from the proposal text:

 std::string s = "but I have heard it works even if you don't believe in it"
 s.replace(0, 4, "").replace(s.find("even"), 4, "only")
  .replace(s.find(" don't"), 6, "");

The example comes from The C++ Programming Language, 4th edition, Stroustrup, and used to be unspecified behaviour, but with C++17 it will work as expected. There were similar issues with resumable functions (.then( . . . )).

As another example, consider the following:

#include <iostream>
#include <string>
#include <vector>
#include <cassert>

struct Speaker{
    int i =0;
    Speaker(std::vector<std::string> words) :words(words) {}
    std::vector<std::string> words;
    std::string operator()(){
        assert(words.size()>0);
        if(i==words.size()) i=0;
        // Pre-C++17 version:
        auto word = words[i] + (i+1==words.size()?"
":",");
        ++i;
        return word;
        // Still not possible with C++17:
        // return words[i++] + (i==words.size()?"
":",");

    }
};

int main() {
    auto spk = Speaker{{"All", "Work", "and", "no", "play"}};
    std::cout << spk() << spk() << spk() << spk() << spk() ;
}

With C++14 and before we may (and will) get results such as

play
no,and,Work,All,

instead of

All,work,and,no,play

Note that the above is in effect the same as

(((((std::cout << spk()) << spk()) << spk()) << spk()) << spk()) ;

But still, before C++17 there was no guarantee that the first calls would come first into the stream.

References: From the accepted proposal:

Postfix expressions are evaluated from left to right. This includes functions calls and member selection expressions.

Assignment expressions are evaluated from right to left. This includes compound assignments.

Operands to shift operators are evaluated from left to right. In summary, the following expressions are evaluated in the order a, then b, then c, then d:

  1. a.b
  2. a->b
  3. a->*b
  4. a(b1, b2, b3)
  5. b @= a
  6. a[b]
  7. a << b
  8. a >> b

Furthermore, we suggest the following additional rule: the order of evaluation of an expression involving an overloaded operator is determined by the order associated with the corresponding built-in operator, not the rules for function calls.

Edit note: My original answer misinterpreted a(b1, b2, b3). The order of b1, b2, b3 is still unspecified. (thank you @KABoissonneault, all commenters.)

However, (as @Yakk points out) and this is important: Even when b1, b2, b3 are non-trivial expressions, each of them are completely evaluated and tied to the respective function parameter before the other ones are started to be evaluated. The standard states this like this:

§5.2.2 - Function call 5.2.2.4:

. . . The postfix-expression is sequenced before each expression in the expression-list and any default argument. Every value computation and side effect associated with the initialization of a parameter, and the initialization itself, is sequenced before every value computation and side effect associated with the initialization of any subsequent parameter.

However, one of these new sentences are missing from the GitHub draft:

Every value computation and side effect associated with the initialization of a parameter, and the initialization itself, is sequenced before every value computation and side effect associated with the initialization of any subsequent parameter.

The example is there. It solves a decades-old problems (as explained by Herb Sutter) with exception safety where things like

f(std::unique_ptr<A> a, std::unique_ptr<B> b);

f(get_raw_a(), get_raw_a());

would leak if one of the calls get_raw_a() would throw before the other raw pointer was tied to its smart pointer parameter.

As pointed out by T.C., the example is flawed since unique_ptr construction from raw pointer is explicit, preventing this from compiling.*

Also note this classical question (tagged C, not C++):

int x=0;
x++ + ++x;

is still undefined.

这篇关于C++17 引入的评估顺序保证是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to enforce move semantics when a vector grows?(当向量增长时如何强制执行移动语义?)
Typedef function pointer?(typedef函数指针?)
Reflection and refraction impossible without recursive ray tracing?(没有递归光线追踪就不可能实现反射和折射?)
Is delete[] equal to delete?(delete[] 是否等于删除?)
Why is unsigned integer overflow defined behavior but signed integer overflow isn#39;t?(为什么定义了无符号整数溢出行为但没有定义有符号整数溢出?)
Unions and type-punning(工会和类型双关语)