std::bind() 参数列表中函子的执行顺序(可能与函数参数的求值顺序无关)

execution order of functors in std::bind() parameter list (maybe not related to evaluation order of function parameter)

我只是想知道并编写了以下代码:(我不是在寻求解决方法,我只是想知道 bind() 是否完美地处理了这个问题)。此外,我认为bind可以处理这个并为其定义一个顺序,但我不确定或者这可能只是一个参数评估顺序问题。

#include <functional>
#include <utility>
#include <iostream>
using namespace std;
class increment{
public : 
    string operator()(int& x)
    {
        ++x;
        return string {"dumb_return_value"};
    }
};

class multiply{
public : 
    string operator()(int& x)
    {
        x = x * 2;
        return string {"dumb_return_value"};
    }
};

template <typename A, typename B>
class do_nothing{
public : 
    void operator()(A , B)
    {
        return;
    }
};


int main()
{
    int x = 0;
    increment increment_object;
    multiply multiply_object;
    do_nothing<string,string> do_nothing_object;

    bind(do_nothing_object,bind(increment_object,ref(x)),bind(multiply_object,ref(x)))();
    cout << x << endl;//output 2

    x = 0;
    bind(do_nothing_object,bind(multiply_object,ref(x)),bind(increment_object,ref(x)))();
    cout << x << endl;//output 1
}

编译选项:std=c++1z 所以,我在这里使用引用语义来放大差异。 incrementmultiply这两个子函数的执行顺序是什么? bind 是谨慎处理并定义顺序,还是取决于特定于编译器的函数参数评估顺序?如果std::bind()处理得当,请引用,谢谢!

PS:我问这个问题的原因之一是:

使用 this draft 作为参考。

func.bind.bind [20.10.9.1.3] 未指定何时评估绑定子表达式或它们的顺序。

它只是说明如果绑定的参数之一是绑定表达式结果,则外部绑定表达式的 return 值使用调用内部绑定表达式的结果以及传递给的参数外部绑定表达式。它没有给出评估这些值的时间顺序信息;人们可能会认为它 "must" 发生在外部绑定表达式被传递参数之后,但是(据我所知)这只是我可以用常识得出的结论,而不是从标准文本本身得出的结论。