iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Automatic type promotion for operations

When building generic functions it is often necessary to determine the return type of a generic method which takes two templated types as input. For example, what should be the return type of the following method?

template<class T1, class T2>
Dims<SOME_TYPE> operator+(Dims<T1> x, Dims<T2> y);

A good solution is to use this nrt::promote class which determines the return type by asking the compiler in which type the result of T1*T2 (which is the largest data type returned by most operations) should be stored. The resulting code looks like this:

template<class T1, class T2>
Dims<nrt::promote<T1,T2>::type> operator+(Dims<T1> x, Dims<T2> y);

Keep in mind that when trying to find an operator* the compiler will first try to match for the exact type, then for any implicit conversions, before getting eaten up by our fail mechanism. So, if you don't indend two types to be promotable but have constructors for one type that takes as an argument the other, make sure you make use of the explicit keyword in your constructors so the compiler can't implicitly convert it. See test-Traits.C


struct  nrt::promote< T1, T2, B >
 Specialization of nrt::promote for when we can't promote. More...
struct  nrt::promote< T1, T2, true >
 Specialization of nrt::promote for when a promotion will occur. More...
struct  nrt::promote< void, T2, false >
 Specialization of nrt::promote when first type is void, promoted type is the second type. More...
struct  nrt::promote< T1, void, false >
 Specialization of nrt::promote when second type is void, promoted type is the first type. More...
struct  nrt::promote< void, void, false >
 Specialization of nrt::promote when both types are void. More...