Solved

cout << operator associativity

Posted on 2013-10-23
8
395 Views
Last Modified: 2013-11-19
All the sources that I have seen say that << operator in cout uses left to right associativity.  But this it doesn't explain the following example.

#include <iostream>
using namespace std;
class A {
public:
	A() { a[0] = 1; a[1] = 0; }
	int a[2];
	int b(void) { int x=a[0]; a[0]=a[1];a[1]=x; return x; }
};
    
int main(void) {
	A a;
	a.b();
	cout << a.b() << endl; 
		cout << a.a[1] << endl;
	return 0;
}

Open in new window


Output is only explained if it is evaluated right to left.

So I made another example and used debugger to see the execution.

#include <iostream>

using namespace std;

int f(int& x)
{
	x++;
    x *= x;
    return x;
}

int main(void)
{
    int a = 2, b=3;
    cout << f(a) << f(b) << endl;
    return 0;
}

Open in new window

I put a break point on function f and sure enough it called f(b) and then f(a).
Why?
0
Comment
Question by:farzanj
8 Comments
 
LVL 39

Accepted Solution

by:
Kyle Abrahams earned 100 total points
ID: 39594218
It boils down to the sequence order is not defined.

There's some great reading about this here:
stackoverflow.com/questions/1504752/c-output-evaluation-order-with-embedded-function-calls
0
 
LVL 31

Author Comment

by:farzanj
ID: 39594223
Sorry my example above incorrect.  Try this:

#include <iostream>
using namespace std;
class A {
public:
	A() { a[0] = 1; a[1] = 0; }
	int a[2];
	int b(void) { int x=a[0]; a[0]=a[1];a[1]=x; return x; }
};
    
int main(void) {
	A a;
	a.b();
	cout << a.b() << a.a[1] << endl;
	return 0;
}

Open in new window

0
 
LVL 39

Assisted Solution

by:Kyle Abrahams
Kyle Abrahams earned 100 total points
ID: 39594229
0
 
LVL 31

Author Comment

by:farzanj
ID: 39594239
I have tried Visual Studio and g++ (GCC) on Linux as well and both have the same output.  If the order is not defined, it is pretty consistent with the compilers.
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 32

Assisted Solution

by:sarabande
sarabande earned 50 total points
ID: 39594641
cout << f(a) << f(b) << endl;

Open in new window


the statement evaluates to

operator<<(operator<<(operator<<(cout, f(a)), f(b)), endl);

Open in new window


that means the arguments of the middle function call are operator<<(cout, f(a)) and f(b). VC compiler would pass the arguments from right to left regardless of the calling convention (see http://msdn.microsoft.com/en-us/library/vstudio/46t77ak2(v=vs.120).aspx) what also means they were evaluated in that order.

so, your observation that f(b) was evaluated before f(a) is true for vc compiler and probably true for most other compilers as well.

Sara
0
 
LVL 22

Assisted Solution

by:ambience
ambience earned 50 total points
ID: 39595429
The answer is what ged325 said:  "sequence order is not defined."

cout << f(a) << f(b) << endl;

is a single expression, and whereas the associativity of the operator is defined, the order of evaluation of functions, temporaries - in a single expression - is up to the compiler (implementation). Even if all compiler writer chose to evaluate in a specific order, it still remains undefined and some day a compiler may choose to do it differently and still not break any rules.

It is therefore bad practice to write code that relies on sequencing of a particular compiler.

Similar reasoning applies to this for example

func( f(a), f(b), f(c) )

There is no defined order in which f(X) would be evaluated.
0
 
LVL 28

Assisted Solution

by:pepr
pepr earned 50 total points
ID: 39596471
In other words, the a.b() is used as a member function for returning a value but with side effects. This is a general problem in any language that allows that.

If you want to be sure that the things are called in the right order, call them as commands outside the cout << ... << end; and assign the results to temporary variables. Then do output the content of the variables. Or design the dedicated member function that has no side effects.
0
 
LVL 31

Author Closing Comment

by:farzanj
ID: 39660422
Thank you guys and sorry for the delay.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
This is about my first experience with programming Arduino.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now