🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: gnu.g++.bug
Article #956

g++ 2.2.2 seems to be forgetting a "const"

#956
From: Marvin Solomon
Date: Mon, 14 Sep 1992 05:09
68 lines
3451 bytes
[gjetost:~/persist/CCversion] cat bug2.c
// g++ 2.2.2 -Wall accepts this program without complaint.  When compiled
// with -pedantic, it says
//   "warning: user-defined conversion casting away `const'"
// In fact, the user-defined conversion is *NOT* casting away the const.
// Rather, it would appear that g++ is using the conversion sequence
//   B -> B& (trivial)
//     -> const B& (trival)
//     -> const A& (user-defined)
//     -> A&  (???)
// Interestingly, the message from cfront 3.0.1 is also misleading:
//   "error: no standard conversion of const A* to A&"
// It's not clear how an A* is ever created.
// Nonetheless, at least cfront properly gives an error.
extern "C" void printf(...);

struct A {
	int i;
};

struct B {
	int i;
	operator const A&() const;
};

B::operator const A&() const {
	static A a;
	a.i = i;
	printf("convert B to A at %x\n", &a);
	return a;
}

void f(A &a) {
	printf("A at %x is %d\n", &a, a.i);
}

main() {
	B b;
	b.i = 99;
	f(b);
}
[gjetost:~/persist/CCversion] g++ -v -Wall -pedantic bug2.c
Reading specs from /usr/gnu/lib/gcc-lib/decstation/2.2.2/specs
gcc version 2.2.2
 /usr/gnu/lib/gcc-lib/decstation/2.2.2/cpp -lang-c++ -v -undef -D__GNUC__=2 -D__GNUG__=2 -D__cplusplus -D__ANSI_COMPAT -DMIPSEL -DR3000 -DSYSTYPE_BSD -D_SYSTYPE_BSD -Dbsd4_2 -Dhost_mips -Dmips -Dultrix -Dunix -D__ANSI_COMPAT -D__MIPSEL__ -D__R3000__ -D__SYSTYPE_BSD__ -D___SYSTYPE_BSD__ -D__bsd4_2__ -D__host_mips__ -D__mips__ -D__ultrix__ -D__unix__ -D__ANSI_COMPAT -D__MIPSEL -D__R3000 -D__SYSTYPE_BSD -D___SYSTYPE_BSD -D__bsd4_2 -D__host_mips -D__mips -D__ultrix -D__unix -Wall -pedantic -D__LANGUAGE_C__ -D_
_LANGUAGE_C -DLANGUAGE_C bug2.c /usr/tmp/cca23102.i
GNU CPP version 2.2.2 [AL 1.1, MM 19] DECstation running ultrix
 /usr/gnu/lib/gcc-lib/decstation/2.2.2/cc1plus /usr/tmp/cca23102.i -quiet -dumpbase bug2.cc -Wall -pedantic -version -o /usr/tmp/cca23102.s
GNU C++ version 2.2.2 [AL 1.1, MM 19] DECstation running ultrix compiled by GNU C version 2.2.2.
bug2.c: In function `int  main ()':
bug2.c:39: warning: user-defined conversion casting away `const'
 as -nocpp -EL -v -o bug2.o /usr/tmp/cca23102.s
/usr/lib/cmplrs/cc/as0 -v -G 8 -EL -g0 -O1 /usr/tmp/cca23102.s -o /tmp/ctmaa23115 -t /tmp/ctmsta23115 
as0: __opRC1A__C1B f__FR1A main 
0.0u 0.1s 0:00 18% 56+56k 0+8io 6pf+0w
/usr/lib/cmplrs/cc/as1 -v -G 8 -p0 -EL -g0 -O1 /tmp/ctmaa23115 -o bug2.o -t /tmp/ctmsta23115 
as1: __opRC1A__C1B f__FR1A main 
0.0u 0.1s 0:00 18% 80+73k 1+1io 8pf+0w
 /usr/gnu/lib/gcc-lib/decstation/2.2.2/mips-tfile -v -o bug2.o /usr/tmp/cca23102.s
mips-tfile version 2.2.2 [AL 1.1, MM 19] DECstation running ultrix
 /usr/gnu/lib/gcc-lib/decstation/2.2.2/ld -EL /usr/lib/cmplrs/cc/crt0.o -L/usr/gnu/lib/gcc-lib/decstation/2.2.2 -L/usr/lib/cmplrs/cc -L/usr/lib/cmplrs/cc -L/usr/gnu/lib bug2.o -lg++ -lgcc -lc -lgcc
[gjetost:~/persist/CCversion] a.out
convert B to A at 100012c0
A at 100012c0 is 99
[gjetost:~/persist/CCversion] exit
exit

script done on Mon Sep 14 10:08:26 1992
Technical Details
Message-ID: <9209141509.AA23124@gjetost.cs.wisc.edu>
Path: arc1-ca.pugleaf.net!sparky!uunet!cis.ohio-state.edu!cs.wisc.edu!solomon

Show Full Headers
Complete Article Headers
Newsgroups: gnu.g++.bug
Path: arc1-ca.pugleaf.net!sparky!uunet!cis.ohio-state.edu!cs.wisc.edu!solomon
From: solomon@cs.wisc.edu (Marvin Solomon)
Subject: g++ 2.2.2 seems to be forgetting a "const"
Message-ID: <9209141509.AA23124@gjetost.cs.wisc.edu>
Sender: gnulists@ai.mit.edu
Organization: GNUs Not Usenet
Distribution: gnu
Date: Mon, 14 Sep 1992 05:09:58 GMT
Approved: bug-g++@prep.ai.mit.edu
Lines: 68