C++/CLI conversion issue

I have two overloaded operators, one taking Object^ and the other taking bool.  I expect the compiler to prefer the conversion from a double to Object^ (boxing) rather than to bool. as the former does not lose information and the latter does.  Is this a valid expection (If so, then perhaps this is a bug).

Consider the following code:

String^ CreateOption( String^ name, Object^ value )
{
   
return name + "->" + value->ToString();
}

#if 1
String^ CreateOption( String^ name,
bool value )
{
  
return value (name + "->True") : (name + "->False");
}
#endif

double d = 1.0;
CreateOption(
"Test", d );

1>c:\sdclient\research\plot\MmaForm.h(484) : warning C4800: 'double' : forcing value to bool 'true' or 'false' (performance warning)

I want the compiler to pick the overload with Object^, and it does so if I were to #if 0 out the other overload.

Brian





Answer this question

C++/CLI conversion issue

  • vhalexxs

    Nevermind.  Docs say:
    A standard conversion will be chosen by the compiler over a conversion that requires boxing.

    The workaround is to explicitly box:
    double d = 1.0;
    CreateOption(
    "Test", (Object^)d );


  • C++/CLI conversion issue