This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
g++ apparent function argument reversal problem
- From: Bill Beckett <wb at typhoon dot graysoft dot com>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Sun, 23 Jun 2002 19:43:26 -0700
- Subject: g++ apparent function argument reversal problem
June 23, 2002
GNU g++,
I believe that you will find that the following program produces a
truly intriguing, however, incorrect result --
#include <iostream>
#include <list>
/* The structure X contains 3 integers, p1, p2, and p3. There is */
/* a three argument constructor which initializes these integers */
/* directly from its arguments. Finally X contains a procedure */
/* p which prints out the integers in order. */
struct X
{
int p1;
int p2;
int p3;
X(int p1, int p2, int p3) : p1(p1), p2(p2), p3(p3) {}
void p()
{
cout << "p1 = " << p1 << endl;
cout << "p2 = " << p2 << endl;
cout << "p3 = " << p3 << endl;
}
};
/* The main program attempts to create an instance of X which is */
/* initialized using the elements of a list of integers. First this */
/* list, l, is declared along with an iterator, i, for accessing its */
/* elements. Then the integers 1, 2, and 3 are successively added */
/* to the end of the list. The iterator is set to the beginning */
/* of the list and the X constructor is called using a series of */
/* expressions which should access the elements of the list in order */
/* from begin() to end(). Finally x.p() is called to print out */
/* the integers stored in the instance. */
/* The program compiles without errors or warnings. But when executed */
/* it produces the following output -- */
/* p1 = 3 */
/* p2 = 2 */
/* p3 = 1 */
/* In other words, somehow, the structure has been initialized as */
/* though the list were reversed! Why do you suppose that is? :) */
/* As an note of possible interest, the Microsoft compiler included */
/* with Visual Studio, version 6, produces the same result. However */
/* the cxx compiler shipped with Compaq Tru64, version 5,1a, produces */
/* a program which prints the numbers in the correct order. */
int main()
{
list <int> l;
list <int>::iterator i;
l.push_back(1);
l.push_back(2);
l.push_back(3);
i = l.begin();
X x
(
*i++,
*i++,
*i++
);
x.p();
}
WB
PS: g++ -v t3.cpp
Reading specs from /usr/local/lib/gcc-lib/i586-pc-linux-gnu/2.95.2/specs
gcc version 2.95.2 19991024 (release)
/usr/local/lib/gcc-lib/i586-pc-linux-gnu/2.95.2/cpp -lang-c++ -v -D__GNUC__=2 -D__GNUG__=2 -D__GNUC_MINOR__=95 -D__cplusplus -D__ELF__ -Dunix -D__i386__ -Dlinux -D__ELF__ -D__unix__ -D__i386__ -D__linux__ -D__unix -D__linux -Asystem(posix) -D__EXCEPTIONS -Acpu(i386) -Amachine(i386) -Di386 -D__i386 -D__i386__ -Di586 -Dpentium -D__i586 -D__i586__ -D__pentium -D__pentium__ t3.cpp /tmp/cctnCdXi.ii
GNU CPP version 2.95.2 19991024 (release) (i386 Linux/ELF)
#include "..." search starts here:
#include <...> search starts here:
/usr/local/lib/gcc-lib/i586-pc-linux-gnu/2.95.2/../../../../include/g++-3
/usr/local/include
/usr/local/lib/gcc-lib/i586-pc-linux-gnu/2.95.2/../../../../i586-pc-linux-gnu/include
/usr/local/lib/gcc-lib/i586-pc-linux-gnu/2.95.2/include
/usr/include
End of search list.
The following default directories have been omitted from the search path:
End of omitted list.
/usr/local/lib/gcc-lib/i586-pc-linux-gnu/2.95.2/cc1plus /tmp/cctnCdXi.ii -quiet -dumpbase t3.cc -version -o /tmp/cc85YkAx.s
GNU C++ version 2.95.2 19991024 (release) (i586-pc-linux-gnu) compiled by GNU C version 2.95.2 19991024 (release).
as -V -Qy -o /tmp/ccsFhwJI.o /tmp/cc85YkAx.s
GNU assembler version 2.9.1 (i386-redhat-linux), using BFD version 2.9.1.0.23
/usr/local/lib/gcc-lib/i586-pc-linux-gnu/2.95.2/collect2 -m elf_i386 -dynamic-linker /lib/ld-linux.so.2 /usr/lib/crt1.o /usr/lib/crti.o /usr/local/lib/gcc-lib/i586-pc-linux-gnu/2.95.2/crtbegin.o -L/usr/local/lib/gcc-lib/i586-pc-linux-gnu/2.95.2 -L/usr/local/lib /tmp/ccsFhwJI.o -lstdc++ -lm -lgcc -lc -lgcc /usr/local/lib/gcc-lib/i586-pc-linux-gnu/2.95.2/crtend.o /usr/lib/crtn.o