C++ 后缀表达式未定义与未指定行为

标签 c++ language-lawyer c++14 undefined-behavior operator-precedence

提前道歉,我知道评估顺序的一般主题已经有很多关于它的问题。但是,在看过它们之后,我想澄清一些我认为不构成重复的具体点。假设我有以下代码:

#include <iostream>

auto myLambda(int& n)
{
    ++n;
    return [](int param) { std::cout << "param: " << param << std::endl; };
}

int main()
{
    int n{0};

    myLambda(n)(n);
    return 0;
}

上面的程序在我编译时输出“n: 0”。在这里,我们有未指定的顺序:它可以很容易地输出“n:1”,但发生了不同的评估顺序。

我的问题是:

  1. 在上面的最终函数调用(即 lambda 表达式调用)期间,后缀表达式 myLambda(0) 之间的排序关系到底是什么? , 它的论点 n ,以及后续的函数调用本身?

  2. 以上是 undefinedunspecified 行为的示例吗?为什么(引用标准)?

  3. 如果我将 lambda 代码更改为 [](int param) { std::cout << "hello" << std::endl } (即,使结果独立于其参数,因此任何评估顺序决策,使行为具有确定性)上面 2)的答案是否仍然相同?

编辑:我已将 lambda 参数名称从“n”更改为“param”,因为这似乎会引起混淆。

最佳答案

具有讽刺意味的是(因为该示例使用 C++11 功能,并且其他答案已被此分散了注意力)使该示例具有未指定行为的逻辑可以追溯到 C++98,第 5 节,第 4 段

Except where noted, the order of evaluation of operands of individual operators and subexpressions of individual expressions, and the order in which side effects take place, is unspecified. Between the previous and next sequence point a scalar object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be accessed only to determine the value to be stored. The requirements of this paragraph shall be met for each allowable ordering of the subexpressions of a full expression; otherwise the behavior is undefined.

基本上相同的子句存在于所有 C++ 标准中,尽管正如 Marc van Leeuwen 的评论所指出的,最近的 C++ 标准不再使用序列点的概念。最终效果是相同的:在语句中,运算符的操作数和单个表达式的子表达式的顺序或求值仍未指定。

出现未指定的行为是因为表达式 n 在语句中被计算了两次

myLambda(n)(n);

表达式 n 的一个评估(以获取引用)与第一个 (n) 和表达式 n 的另一个评估相关联>(获取值)与第二个(n)相关联。这两个表达式的求值顺序(即使它们在光学上都是 n)是未指定的。

类似的子句存在于所有 C++ 标准中,并且具有相同的结果 - 语句 myLambda(n)(n) 上的未指定行为,无论 myLambda() 如何实现

例如,myLambda() 可以像这样在 C++98(以及所有后来的 C++ 标准,包括 C++11 和更高版本)中实现

 class functor
 {
      functor() {};
      int operator()(int n) { std::cout << "n: " << n << std::endl; };
 };

 functor myLambda(int &n) 
 {
       ++n;
       return functor();
 }

 int main()
 {
      int n = 0;

      myLambda(n)(n);
      return 0;
 }

因为问题中的代码只是实现与此相同效果的 (C++11) 技术(或简写)。

以上回答了OP的问题1和2。未指定的行为发生在main()中,与myLambda()本身的实现方式无关。

要回答 OP 的第三个问题,如果 lambda(或我的示例中的仿函数的 operator())被修改为不访问其参数的值,则行为仍未指定。唯一的区别是整个程序不会产生可见的输出,这可能会因编译器而异。

关于C++ 后缀表达式未定义与未指定行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37344370/

相关文章:

c++ - 休眠一个异步任务

c++ - MPI 内存分配

c++ - 如何使用 C++ 静态类方法创建 C 风格的回调

c++ - 存储稍后转发的变量参数

c - 这些是 C 中兼容的函数类型吗?

c++ - 无法在 TDM-GCC 的 typeid 中使用 declval

c++ - C++ 中的模板仅在 long 数据类型上出错

c++ - 超出范围的枚举转换能否产生基础类型之外的值?

c++ - 在子语句的最外层 block 中重新声明的变量

c++ - 绑定(bind)元函数 : accept both types and template template parameters (accept anything)