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

Benjamin Kosnik bkoz@redhat.com
Mon Dec 11 12:08:00 GMT 2000


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



More information about the Gcc-bugs mailing list