www.digitalmars.com         C & C++   DMDScript  

c++ - Compiler doesn't do type conversion of operator?: operands

reply Vaclav Slavik <vslavik fastmail.fm> writes:
Hi,

here's another bug affecting wxWidgets development version: if the types of
operator?:'s operands differ, the compiler doesn't try to convert them to a
common type. Interestingly enough, it *does* do conversion in some cases (i.e.
is already better than BCC5.5, which never converts), but not in others.
Again, this is with the latest stable as well as 8.50 beta versions.

The following code demonstrates it:
--------------
struct wxCStrData
{
    wxCStrData(char *buf) {}
    operator const char*() const { return "x"; }
};

void foo(int n)
{
    wxCStrData c("x");
    const char *x = (n==1) ? c : "bar";
}
--------------

The error given by DMC ("dmc test.cpp") is:

    const char *x = (n==1) ? c : "bar";
                                      ^
dmc_ternary_operator.cpp(10) : Error: illegal operand types
Had: wxCStrData
and: char const *
--- errorlevel 1

The code compiles with all other compilers supported by wx (except BCC5.5,
which has really touchy operator?:) as well as Comeau in strict mode.

Regards,
Vaclav
Mar 16 2007
parent Walter Bright <newshound digitalmars.com> writes:
Thanks for the reports, I'll take care of them.
Mar 18 2007