PromotionTraits is a technique which enables the return type of a function to be determined by the function arguments. This is described in
CppTemplatesTheCompleteGuide, where working code is given. --
JohnFletcher
Example of function definition
template <typename T1, typename T2>
typename Promotion<T1, T2>::ResultT operator+ ( T1 const &, T2 const &);
A word of warning. Use of this can have side effects. I have had a problem where code to do with standard vectors would not work correctly and this turned out to be due to picking up an incorrect operator+. If
V is a vector then
V[n] is implemented for
GnuCpp using
*(V.begin()+n) which involves addition to the iterator. --
JohnFletcher
Does it touch such issues as subclasses of a common super-class (and finding most-derived common superclass)? That gets to be pretty damn difficult, though doable if you design specializations that indicate all superclasses of a class.
Related to
OperatorOverloading
April 2012 Now that
CeePlusPlusEleven is available I am planning to phase out use of this as soon as possible in favour of
auto and
decltype which can do a better job as there is no need to set up macros for the cases needed. The only case I cannot solve so far is one involving
VariadicTemplates. --
JohnFletcher
(For those interested, an example of the above using C++11:)
template <class T1, class T2>
auto operator+(const T1& l, const T2& r) -> decltype(l + r);
CategoryCppTemplates CategoryCpp