This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: c++/328
- To: ruediger dot franke at de dot abb dot com
- Subject: Re: c++/328
- From: Martin von Loewis <loewis at informatik dot hu-berlin dot de>
- Date: Fri, 7 Jul 2000 17:58:37 +0200 (MET DST)
- CC: gcc at gcc dot gnu dot org
- References: <C1256915.00416E99.00@abb-despk-smtp.deinf.abb.de>
[moved to gcc]
> But is there a way to avoid the compiler warning for the example, either via
> reformulation or with a compiler flag?
In many cases, it is possible to correct the code by adding a third
conversion operator. It seems that in the case of returning a
reference, adding this third operator won't help.
In your example, changing the code to read
operator double&() {return _d;}
operator double() const {return _d;}
makes it produce no warning under -pedantic; I'm not sure why it does
the wrong thing without -pedantic.
> Isn't the example valid C++ code and the warning just tells me how
> gcc optimizes the code?
No. The compiler tells you that it choses the operator returning
double& even when a "const double&" is expected. This is in full
conformance with the standard, but many people expect that it should
invoke the operator returning "const double&". They are then surprised
when the compiler, at run-time, picks the seemingly wrong function.
So this warning is meant to educate users that their code is easily
misunderstood. It has been quite successful in doing so.
The mainline compiler (gcc version 2.96 20000624 (experimental)) does
not produce this warning anymore, so I guess I can just close this
report.
Regards,
Martin