Streaming and Order of Precedence (II)

S

Snorri

Sorry, think I forgot to sign up before I sent the last post and the
message got deleted. Copied its contents below.

Hi all,

I've recently had some issues with the streaming operator and the
order of precedence used when mixing it with the member function
operator (operator .) Example code is below.

*************************************************************************
#include <iostream>
#include <vector>
using namespace std;

class A
{
public:
A() : i(0) {};
int f() {
i++;
v.push_back(i);
return i;
}

void reset() { i = 0; v.clear(); }

int i;
vector<int> v;
};

int main()
{
A a;

// Order of action:
// - Member functions fire from RIGHT to LEFT.
// - Insertion operators fire from LEFT to RIGHT
cout << a.f() << a.f();
cout << endl;

cout << "Vector contents a: ";
for (unsigned int i = 0; i < a.v.size(); ++i )
{
cout << a.v.at(i);
}
cout << endl;

a.reset();
int k;
// Order of action;
// - Member functions fire from LEFT to RIGHT
// - Addition operator fires.
// - Assignment operator fires.
k = a.f() + 3*a.f();
cout << "k: " << k << endl;
cout << "Vector contents a: ";
for (unsigned int i = 0; i < a.v.size(); ++i )
{
cout << a.v.at(i);
}
return 0;
}
*********************************************************************************
OUTPUT:
21
Vector contents a: 12
k: 7
Vector contents a: 12
*********************************************************************************

The result of this seems to indicate that the . operator for the
member function call has different associativities in the different
situations. I must be missing something here, but I haven't been able
to find anyone to give me a conclusive answer on it. Can anyone here
shed some light on it?

Cheers,
Daniel.
 
V

Victor Bazarov

Snorri said:
Sorry, think I forgot to sign up before I sent the last post and the
message got deleted. Copied its contents below.

Hi all,

I've recently had some issues with the streaming operator and the
order of precedence used when mixing it with the member function
operator (operator .) Example code is below.

*************************************************************************
#include <iostream>
#include <vector>
using namespace std;

class A
{
public:
A() : i(0) {};
int f() {
i++;
v.push_back(i);
return i;
}

void reset() { i = 0; v.clear(); }

int i;
vector<int> v;
};

int main()
{
A a;

// Order of action:
// - Member functions fire from RIGHT to LEFT.
Huh?

// - Insertion operators fire from LEFT to RIGHT
cout << a.f() << a.f();

If you're trying to rely on a certain _order_ of two different
function calls, you're *wrong*. The order is _unspecified_.
cout << endl;

cout << "Vector contents a: ";
for (unsigned int i = 0; i < a.v.size(); ++i )
{
cout << a.v.at(i);
}
cout << endl;

a.reset();
int k;
// Order of action;
// - Member functions fire from LEFT to RIGHT
// - Addition operator fires.
// - Assignment operator fires.
k = a.f() + 3*a.f();
cout << "k: " << k << endl;
cout << "Vector contents a: ";
for (unsigned int i = 0; i < a.v.size(); ++i )
{
cout << a.v.at(i);
}
return 0;
}
*********************************************************************************
OUTPUT:
21
Vector contents a: 12
k: 7
Vector contents a: 12
*********************************************************************************

The result of this seems to indicate that the . operator for the
member function call has different associativities in the different
situations. I must be missing something here, but I haven't been able
to find anyone to give me a conclusive answer on it. Can anyone here
shed some light on it?

You seem to be running into the _unspecifiedness_ of the order of
evaluation of function arguments.

Imagine that you have

void foo(int a, int b);

and then somewhere you would have

foo(a.f(), a.f());

what would the values of 'a' and 'b' be in 'foo'?

The answer is - they would be different, but there is no way to
tell whether the 'a' is going to be greater or the 'b'.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top