I understand that when I call a function such as
a(b(),c());
then the behavior of this may be undefined in <= C++14, and unspecified in >= C++17, in the sense that it is up to the compiler to determine whether to evaluate b
or c
first.
I would like to know the best way to force an evaluation order. I will be compiling as C++14.
The thing that immediately comes to mind is something like this:
#include <iostream>
int count = 5;
auto increment(){
return count++;
}
template <typename A, typename B>
auto diff(A && a, B && b){
return a - b;
}
int main() {
auto && a = increment();
auto && b = increment();
auto c = diff(a,b);
}
Am I in undefined behavior land? Or is this how one is "supposed" to force evaluation order?
auto && a = bar();
and then passing that to another function. – Fastigiumb()
and thenc()
, orc()
and thenb()
. One of them will happen. It is not specified, which. – Armenian