Bug report

Martin v. Loewis martin@mira.isdn.cs.tu-berlin.de
Wed Mar 31 23:54:00 GMT 1999


> I think I have found a compiler bug, look in attachment for details.
> There are two C++ source files - 'test.cc' and 'test_new.cc'

I couldn't find any difference in the files you sent; your description
was clear enough to let me understand your problem.

> Seems that when I am calling an operator with 'const' modifier and
> there are one more operator exist without any 'const', then it will
> call last one instead of what I am wishing to be called.

Just so we are sure what we are talking about, I include the relevant
part of your code:

class a
{
 public:
    operator bool() const { printf( "bool operator\n" ); return true; }
    operator int() { printf( "int operator\n" ); return 1; }
};

int
main()
{
        a a1;
        bool b = a1;
}

In this case, operator int() is called, instead of 
operator bool()const. This might be confusing, but is correct:

Both operators can be used to initialize b, because there is a
standard conversion from int to bool. So which one is better?
This is decided by overload resolution. 

We look at the arguments: there is one argument, of type 'class
a*'. For operator int, we can directly call the conversion
function. For operator bool, we have to convert 'class a*' to 'const
class a*' (qualification conversion). So the function with less
conversion is called, which is operator int.

If you want the conversion work as expected, you also have to provide
an "operator bool();". Both this operator and the operator int() are
identical in terms of argument conversion, but the bool operator gives
the better result.

Hope this helps,
Martin



More information about the Gcc-bugs mailing list