5

Can somebody quote the corresponding paragraph of the C++ standard that says that the order of the std::string construction and foo() call is unspecified in the following case:

std::string().append(foo());

I know that there's 5.2.2.8 but it states about function arguments, not several function calls between the same sequence points:

The order of evaluation of function arguments is unspecified

curiousguy
  • 8,038
  • 2
  • 40
  • 58
FrozenHeart
  • 19,844
  • 33
  • 126
  • 242
  • 1
    [5.2.2.5](http://eel.is/c++draft/expr.post#expr.call-4) "If the function is a non-static member function, the this parameter of the function ([class.this]) shall be initialized with a pointer to the object of the call, converted as if by an explicit type conversion ([expr.cast])." seems to indicate it's treated as the source of the implied `this` parameter for the function, but I wouldn't put money on my reading being correct, since I've not thoroughly read this part of the standard. – jaggedSpire Jun 07 '16 at 21:25

2 Answers2

14

It was widely believed that leaving the order of expression evaluation undefined led to more optimizations. It was probably true 10 and 20 years ago, but it does not appear to be so any more. Data to that effect was presented to the committee, but I don't know if it is published anywhere.

3

5/4 [expr]:

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

Because operators are functions, this means string() will be constructued either before or after foo().

Also see: Order of function call

"Bjarne Stroustrup also says it explicitly in "The C++ Programming Language" 3rd edition section 6.2.2, with some reasoning:

Better code can be generated in the absence of restrictions on expression evaluation order.

Also see: Order of evaluation in C++ function parameters

Community
  • 1
  • 1
Jossie Calderon
  • 1,393
  • 12
  • 21