By FailedDev

2011-10-10 20:49:22 8 Comments

Hi all I stumbled upon this piece of code today and I am confused as to what exactly happens and more particular in what order :

Code :

#include <iostream>

bool foo(double & m)
    m = 1.0;
    return true;

int main()
    double test = 0.0;
    std::cout << "Value of test is : \t" << test << "\tReturn value of function is : " << foo(test) <<  "\tValue of test : " << test << std::endl;
    return 0;

The output is :

Value of test is :      1       Return value of function is : 1 Value of test : 0

Seeing this I would assume that somehow the right most argument is printed before the call to the function. So this is right to left evaluation?? During debugging though it seems that the function is called prior to the output which is what I would expect. I am using Win7 and MSVS 2010. Any help is appreciated!


@M.M 2018-05-16 02:14:55

The answer to this question changed in C++17.

Evaluation of overloaded operators are now sequenced in the same way as for built-in operators (C++17 [over.match.oper]/2).

Furthermore, the <<, >> and subscripting operators now have the left operand sequenced before the right, and the postfix-expression of a function call is sequenced before evaluation of the arguments.

(The other binary operators retain their previous sequencing, e.g. + is still unsequenced).

So the code in the question must now output Value of test is : 0 Return value of function is : 1 Value of test : 1. But the advice "Don't do this" is still reasonable, given that it will take some time for everybody to update to C++17.

@Matteo Italia 2011-10-10 20:52:15

The evaluation order of elements in an expression is unspecified (except some very particular cases, such as the && and || operators and the ternary operator, which introduce sequence points); so, it's not guaranteed that test will be evaluated before or after foo(test) (which modifies it).

If your code relies on a particular order of evaluation the simplest method to obtain it is to split your expression in several separated statements.

@alfC 2016-05-20 23:12:54

Do you know of a trick using || or && to enforce a certain evaluation order in a << chain?

@Matteo Italia 2016-05-21 03:25:56

No, but I know of a trick using ; and repeating the stream variable after a newline ;-)

@Goran Flegar 2018-05-16 02:12:26

Maybe updating this answer with the changes introduced in the C++17 standard would be helpful for people stumbling upon this in the future:

@duncan 2011-10-10 21:02:49

The order of evaluation is unspecified, see

This is the same situation as the example with the operator+ example:

Consider two functions f() and g(). In C and C++, the + operator is not associated with a sequence point, and therefore in the expression f()+g() it is possible that either f() or g() will be executed first.

@John Dibling 2011-10-10 21:18:18

Not undefined. Unspecified. There's a big difference.

@duncan 2011-10-10 21:48:12

Right, fixed. Thanks.

@John Dibling 2011-10-10 20:53:49

Order of evaluation is unspecified. It is not left-to-right, right-to-left, or anything else.

Don't do this.

@Thomas Matthews 2011-10-11 00:20:04

+1: "Don't do this."

Related Questions

Sponsored Content

36 Answered Questions

[SOLVED] Why is "using namespace std" considered bad practice?

25 Answered Questions

[SOLVED] How to convert std::string to lower case?

13 Answered Questions

[SOLVED] Converting from a std::string to bool

  • 2010-01-29 23:19:46
  • cquillen
  • 74853 View
  • 45 Score
  • 13 Answer
  • Tags:   c++ string boolean

40 Answered Questions

[SOLVED] What's the best way to trim std::string?

  • 2008-10-19 19:23:07
  • Milan Babuškov
  • 604765 View
  • 717 Score
  • 40 Answer
  • Tags:   c++ trim stdstring

12 Answered Questions

[SOLVED] std::wstring VS std::string

8 Answered Questions

[SOLVED] How to convert a std::string to const char* or char*?

  • 2008-12-07 19:30:56
  • user37875
  • 860237 View
  • 837 Score
  • 8 Answer
  • Tags:   c++ string char const

1 Answered Questions

[SOLVED] cout a stringstream but print a pointer

  • 2017-01-02 10:47:13
  • 杜智超
  • 421 View
  • 0 Score
  • 1 Answer
  • Tags:   c++ c++03

2 Answered Questions

3 Answered Questions

[SOLVED] Trouble with std::ostringstream as function parameter

2 Answered Questions

[SOLVED] setw within a function to return an ostream

Sponsored Content