Can we have a function with multiple return types? (in C++11 and above)

The return type must be determined at compile-time. You might use std::common_type (since C++11):

For arithmetic types not subject to promotion, the common type may be viewed as the type of the (possibly mixed-mode) arithmetic expression such as T0() + T1() + ... + Tn().

template < typename T, typename X>
typename std::common_type<T, X>::type Max_Number ( T valueA, X valueB ) {
    ...
}

Or use std::conditional (since C++11) to declare the return type as the big one (whose sizeof is greater).

template < typename T, typename X>
typename std::conditional<sizeof(T) >= sizeof(X), T, X>::type Max_Number ( T valueA, X valueB ) {
    ...
}

Note that for this case, if T and X have the same size, T will always be used as the return type. If you want to control it more precisely, you can use some traits to specify the exact type.

Leave a Comment