I've been playing around with a set of templates for determining the correct promotion type given two primitive types in C++. The idea is that if you define a custom numeric template, you could use these to determine the return type of, say, the operator+ function based on the class passed to the templates. For example:
// Custom numeric class
template <class T>
struct Complex {
Complex(T real, T imag) : r(real), i(imag) {}
T r, i;
// Other implementation stuff
};
// Generic arithmetic promotion template
template <class T, class U>
struct ArithmeticPromotion {
typedef typename X type; // I realize this is incorrect, but the point is it would
// figure out what X would be via trait testing, etc
};
// Specialization of arithmetic promotion template
template <>
class ArithmeticPromotion<long long, unsigned long> {
typedef typename unsigned long long type;
}
// Arithmetic promotion template actually being used
template <class T, class U>
Complex<typename ArithmeticPromotion<T, U>::type>
operator+ (Complex<T>& lhs, Complex<U>& rhs) {
return Complex<typename ArithmeticPromotion<T, U>::type>(lhs.r + rhs.r, lhs.i + rhs.i);
}
If you use these promotion templates, you can more or less treat your user defined types as if they're primitives with the same promotion rules being applied to them. So, I guess the question I have is would this be something that could be useful? And if so, what sorts of common tasks would you want templated out for ease of use? I'm working on the assumption that just having the promotion templates alone would be insufficient for practical adoption.
Incidentally, Boost has something similar in its math/tools/promotion header, but it's really more for getting values ready to be passed to the standard C math functions (that expect either 2 ints or 2 doubles) and bypasses all of the integral types. Is something that simple preferable to having complete control over how your objects are being converted?
TL;DR: What sorts of helper templates would you expect to find in an arithmetic promotion header beyond the machinery that does the promotion itself?