开发者

When to use conversion operators?

Considering the following example, the line int a = objT + 5; gives ambiguous conversion which is handled in two ways, using the explicit cast which i think shouldn't be necessary and replacing the use of conversion operators with member functions instead. and here, my question becomes should you s开发者_开发知识库till use conversions operators or not at all?

class Testable {

public:
    Testable(int val = 0):x(val){}

    operator int() const  { return x; }
    operator double() const  { return x; }

    int toInt() { return x; }
    double toDouble() { return x; }

private:
    int x;
};

int main()
{
    Testable objT(10);

    // ambiguous conversion
    int a = objT + 5;

    // why to use explicit cast when
    // the conversion operator is for to handle
    // this automatically
    int b = static_cast<int>(objT) + 5;

    // member functions
    int c = objT.toInt() + 5;
}


Note that int d = objT; is unambiguous, as is double e = objT; Here the compiler can unambiguously choose a conversion operator because of the exact match between the type of the left hand side and the return types from those conversion operators.

The ambiguous conversion results from objT + 5 (also note: long f = objT; is also ambiguous). The problem is that you've taken away the essential information the compiler needs to mindlessly perform the disambiguation.

The problem goes away if you get rid of either of those conversion operators. Since the underlying data member is an int, I suggest getting rid of operator double().


For your particular case, you could just provide the conversion to double, since it has a standard conversion to int. However, implicit conversions often do more harm than help. If you use them, then avoid having both the conversion from int (your non explicit constructor) and to int, which I think is the source of your current problem.


This question is similar to mine, whether is it possible to change the precedence of implicit conversion operators. Unfortunately there is no satisfactory solution, there is no precedence amongst implicit conversions.

One solution is to have additional overload(s) to the ambiguous function or operator, in your case they are operator + (const Testable&, int) and operator + (const Testable&, double).

Another solution is to simply leave only one implicit conversion, in your case the integer one would be best.

Only use implicit conversion when you desperately need automatic conversion to the given type. Explicit toInt, toDouble functions are MUCH better, they clarify the code and do not hide potential pitfalls. Use the explicit keyword for unary constructors, they block implicit conversion via that constructor.

And no, it is not possible to chain implicit conversions, neither if they are operators nor constructors. The C++ standard mandates only 1 implicit conversion can be in a conversion sequence.


You don't need implicit conversion to double. You always return decimal value and compiler can combine multiple implicit conversion operators.

If your private property x of type int were a double, I would recommend not to use implicit conversion to int since it would cause precision loss.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜