This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: c++/1039: cout does not give correct output with pointer ++



for the following:

#include <iostream>

int main()
{
  using namespace std;
  int i = 5;
  int* p = &i;
  int i2 = 5;
  int* p2 = &i;
  // 1
  cout << "combined ";
  cout << p <<  ":" << *p <<  ":" << p++ <<  ":" << *p++ << endl;

  // 2
  cout << "separate ";
  cout << p2 << ":" << *p2 << ":";
  cout << p2++ << ":" << *p2++ << endl;
  return 0;
}

I get, with current CVS on x86/linux:

%a.out 
combined 0xbffffacc:134523401:0xbffffac8:5
separate 0xbffffac4:5:0xbffffac8:5

This is actually correct: look at precedence rules for operators, and
the way iostream operators are constructed.

ie, 

cout << p2 << ":" is equivalent to
(cout.operator<<p2)).operator<<(":")

-benjamin


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]