Noisy when choosing T::operator Type() vs T::op const Type() const

Jim Carter jimc@math.ucla.edu
Sun Sep 10 16:59:00 GMT 2000


// bug01.C -- Noisy when choosing T::operator Type() vs T::op const Type() const
/*
Type T can be converted to B by T::operator B&(), but there's also a conversion
T::operator const B&() const.  (Similarly for pointers.)  In a situation where
non-const T is available but const B is wanted, gcc could choose either
conversion.  It considers T::operator B&() to be better, probably correctly.
The complaint is, it issues a warning message.  If it knows T::operator B&() is
better, it should shut up and use it!

(Feature request: a pragma that could cause the compiler to spit out, only for
selected small code sections, what it thinks it's trying to accomplish, what
functions are candidates, which one it chose, and why.  In other words,
preserve this bug plus other similar warning messages, but only when the user
engages the pragma.)

Version:	gcc v2.95.2
Configure:	--prefix=/usr/local --enable-shared \
		--enable-languages=c++,f77,java
Environment:	i686-pc-linux-gnu 
		(Slackware 7.1, kernel 2.2.16, Intel Pentium 3)
Command line:	g++ bug01.C
Submitted-date:	2000-09-10
To:		gcc-bugs@gcc.gnu.org
Submitter:	James F. Carter <jimc@math.ucla.edu>

James F. Carter        Voice 310 825 2897	FAX 310 206 6673
UCLA-Mathnet;  6115 MSA; 405 Hilgard Ave.; Los Angeles, CA, USA  90095-1555
Internet: jimc@math.ucla.edu (finger for PGP key)
UUCP:...!{ucsd,ames,ncar,gatech,purdue,rutgers,decvax,uunet}!math.ucla.edu!jimc

Output complained about:

bug01.C: In function `int main()':
bug01.C:44: warning: choosing `T::operator int &()' over `T::operator const int &() const'
bug01.C:44: warning:   for conversion from `T' to `int'
bug01.C:44: warning:   because conversion sequence for the argument is better
*/

class T {
  private:
    int content;
  public:
    T() { }
    T(int c) : content(c) { }
    T(const T& t) : content(t.content) { }
    
    operator int& () { return content; }
    operator const int& () const { return content; }
};

int main() {
    T t(0);
    int c = t;		// <== Message here
    return c;
}




More information about the Gcc-bugs mailing list