Hi,

When I overload, say, the + operator for a class, I usually return an instance of that class following the + operation. However in principle one might also return an instance of the type that has been added to the class (which might make sense in numerical computations if one wishes to avoid loss of precision). C++ appears to make this choice automatically. For example, try the following code (using the tr1 auto):

Code:
    int a = 3;
    double b = 3.9;
    auto k = a + b;
    cout << typeid(k).name() << endl; // k is a double
I was wondering if anybody could tell me how the compiler decides to make k a double rather than an int in this case, as I would like to similarly return the most precise type from an addition operator in a custom complex number class.

For example,

Code:
    complex<int> a (3,3);
    complex<double> b(4,4);
    auto k = a + b;
    // I would like k to be a complex<double>, not a complex<int>, but my overload of + returns the latter. I need to decide dynamically at compile time what the appropriate result is!
Is there any way to access information on the "type precedence" (for want of a better phrase) or do I need to go through all integral types and work out the correct precedence for the + operator? I gather I could put together a type_traits kind of class which typedefs the appropriate result from a binary operation on two different types, but is there an easier way to get the same effect?

Thanks!