This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: c++/328


[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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]