This is the mail archive of the gcc-bugs@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++ conversion rules ???



> >>>>> Charles Galambos <ees1cg@ee.surrey.ac.uk> writes:
> 
>  > I'm not sure I understand what your suggesting, but the options I can
>  > see are as follows:
> 
>  > If I add a non-const 'const char*Func()', then I'd loose non-constant
>  > access to the class member.
> 
> The idea was to have
> 
>   operator const char * () const { return foo; }
>   operator char * () { return bar; }
>   operator const char * () { return operator char*(); }
> 
> You wouldn't lose non-constant access.
> 
> Jason

Ok, This works.  Its not much effort to change. So as a g++ user I'd say its
worth it to have the warning in less clear circumstances elsewhere.  

Here's another case that's I can't solve in this way,

I have a class that I use in some circumstances instead of a plain bool, it 
lets
me change the behaviour of some operators, set default values on construction 
etc..

Originaly I had something like,

class MyBoolean
{
public:
  operator bool &() { return x; }
  operator bool () const { return x; }
  
  bool x;
};

if I try:

 MyBoolean x ;
 bool v = x;

as with the prevous problem I get:

test.cc: In function `int func()':
test.cc:16: warning: choosing `MyBoolean::operator bool &()' over 
`MyBoolean::operator bool() const'
test.cc:16: warning:   for conversion from `MyBoolean' to `bool'
test.cc:16: warning:   because conversion sequence for the argument is better


Now I add  an operator in a similar way to that which was suggested for the 
first problem

  operator bool () { return x; }

and get
test.cc:17: conversion from `MyBoolean' to `bool' is ambiguous
test.cc:5: candidates are: MyBoolean::operator bool &()
test.cc:6:                 MyBoolean::operator bool() const
test.cc:7:                 MyBoolean::operator bool()

I've also tried several other combination of operators and get similar 
problems.

As a c++ user I like warnings they've saved me plenty of grief, but they're 
only useful
if there's a clean way to get rid of them.

Hope this helps, and thanks.

Charles.









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