www.digitalmars.com         C & C++   DMDScript  

c++ - C++ compiler incorrectly reports ambiguous type conversion on bool operator


I'm experiencing the following bug in DMC (both latest stable and 8.50 beta)
after making some extensive changes to the wxWidgets library API: there's a
class, wxUniChar, that has implicit conversion operators to char, wchar_t and
bool, the latter in order to be able to use the class in expression like "if
(c) ..." or "if (*p) ...". Unfortunately, DMC fails to compile such code with
"ambiguous type conversion" error even though there's explicit operator bool
defined in the class.

Here's a trimmed down example that demonstrates it:
struct wxUniChar
    wxUniChar(char v) {}

    operator char() const { return 'a'; }
    operator bool() const { return true; }

int main()
    wxUniChar c('a');
    if ( c )
        return 0;
        return 1;

This code fails with the following compiler error (running it as just "dmc

    if ( c )
dmc_operator_bool.cpp(12) : Error: ambiguous type conversion
--- errorlevel 1

The code compiles fines with all other compilers I tried, including Comeau in
strict mode and GCC 4.1. Replacing the if expression with "if ( (bool)c )"
fixes it for DMC.

While the bug is not too serious for wxWidgets itself (only two places where
it broke compilation), it negatively impacts backward compatibility of wx3 for
DMC users.

Mar 16 2007