public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Re: c++/328
       [not found] <C1256915.00416E99.00@abb-despk-smtp.deinf.abb.de>
@ 2000-07-07  8:58 ` Martin von Loewis
  0 siblings, 0 replies; only message in thread
From: Martin von Loewis @ 2000-07-07  8:58 UTC (permalink / raw)
  To: ruediger.franke; +Cc: gcc

[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

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2000-07-07  8:58 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <C1256915.00416E99.00@abb-despk-smtp.deinf.abb.de>
2000-07-07  8:58 ` c++/328 Martin von Loewis

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).