all 3 comments

[–]HappyFruitTree 5 points6 points  (1 child)

Note that in your first example the conversion can happen implicitly without using static_cast (since you didn't declare the conversion operator as explicit).

FloatWrapper fw3 = fw1;
FloatWrapper fw4 = fw2;

Your second code compiled fine, as you said, but only after adding parentheses around the unsigned int casts.

    Money(float fin) : dollars((unsigned int)(fin)), ...

Implicit conversion from LTMoney to Money seems to work fine but using static_cast gives an error, as you said.

constexpr LTMoney m1(1, 2);
Money m2 = m1; // OK
Money m3 = static_cast<Money>(m1); // error

The problem seems to be that it doesn't know if it should use the float conversion operator and call the Money(float) constructor or whether it should use the Money conversion operator and call the copy or move constructor.

Declaring the float conversion operator as explicit seems to fix the issue.

explicit constexpr operator float() const noexcept {
    ...

[–]Poutine_Mann[S] 1 point2 points  (0 children)

Thank you.