Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Assign to implicitly converted lvalue

Tags:

People also ask

What is implicit conversion in C?

Implicit type conversion in C language is the conversion of one data type into another datatype by the compiler during the execution of the program. It is also called automatic type conversion.

What happens in 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 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 the use of type conversion write the standard conversion rules in C++?

Type Conversion in C++ Done by the compiler on its own, without any external trigger from the user. Generally takes place when in an expression more than one data type is present. In such condition type conversion (type promotion) takes place to avoid lose of data.


Consider this snippet of C++ code:

struct Foo {
    float value;

    operator float& () {
        return this->value;
    }
};

int main() {
    Foo foo;
    foo=1.0f;   //Doesn't compile, foo isn't implicitly converted to a float&

    return 0;
}

Why doesn't this compile? Is there a specific reason this wasn't included in the C++ standard? Or an equivalent does indeed exist and I'm just using it wrong?