Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implicit conversion in C++

In C++, I'm trying to use implicit conversion with a conditional operator. Consider this example:

class MyFloat
{ 
    public:                                                                    
        MyFloat(float val){m_val = val;}
        operator float(){return m_val;}
    protected:
        float m_val;
};

int main(int argc, char **argv)
{
    MyFloat a = 0.5f;
    MyFloat b = 1.0f;                            
    float x = true ? a-0.5f : b;
    return 0;
}

It causes a compiler error:

error: operands to ?: have different types ‘MyFloat’ and ‘float’

I expect the conditional operator to implicitly convert b to the type of a-0.5, float. But this does not happen. How do I achieve this implicit cast?

Ideally, I want to avoid a static cast or an accessor method like float MyFloat::getValue().

like image 881
ethan Avatar asked Feb 06 '13 21:02

ethan


People also ask

What is implicit conversion give an example?

Implicit conversions: No special syntax is required because the conversion always succeeds and no data will be lost. Examples include conversions from smaller to larger integral types, and conversions from derived classes to base classes.

What is meant by implicit conversion?

An implicit conversion sequence is the sequence of conversions required to convert an argument in a function call to the type of the corresponding parameter in a function declaration. The compiler tries to determine an implicit conversion sequence for each argument.

What is explicit type conversion?

Explicit type conversion, also called type casting, is a type conversion which is explicitly defined within a program (instead of being done automatically according to the rules of the language for implicit type conversion). It is defined by the user in the program.

How many types of conversion are there in C?

There are two types of conversion: implicit and explicit. The term for implicit type conversion is coercion.


2 Answers

The problem is that there are two conversions. The compiler can convert a-0.5 to MyFloat or it can convert b to float. As long as you have both conversions and neither is marked explicit you'll get this kind of ambiguity all the time.

like image 126
Pete Becker Avatar answered Sep 20 '22 09:09

Pete Becker


Only some conversions are done for you. From http://msdn.microsoft.com/en-us/library/e4213hs1(v=vs.71).aspx

The first operand must be of integral or pointer type. The following rules apply to the second and third expressions:

  • If both expressions are of the same type, the result is of that type.
  • If both expressions are of arithmetic or enumeration types, the usual arithmetic - conversions (covered in Arithmetic Conversions) are performed to convert them to a common type.
  • If both expressions are of pointer types or if one is a pointer type and the other is a constant expression that evaluates to 0, pointer conversions are performed to convert them to a common type.
  • If both expressions are of reference types, reference conversions are performed to convert them to a common type.
  • If both expressions are of type void, the common type is type void.
  • If both expressions are of a given class type, the common type is that class type.
like image 44
krsteeve Avatar answered Sep 20 '22 09:09

krsteeve