我一直在玩一套模板,用于在C++中给出两种原始类型来确定正确的促销类型.这个想法是,如果你定义一个自定义数字模板,你可以使用它们来确定,例如,基于传递给模板的类的操作符+函数的返回类型.例如:
// 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 …
Run Code Online (Sandbox Code Playgroud)