In this post I'm considering the problem of checking if a type exactly matches another, it's not exactly what is asked for, but it is simpler and I hope it can help to understand the applied template tricks.
As done in boost, template specializations can be adopted for that task, in fact you can define a template struct containing operations on a given type, and use nested template structs for that operations. In our case:
// Working on a specific type:
template <typename T1>
struct is_type {
// For all types T2!=T1 produce false:
template <typename T2>
struct same_of { static const bool value = false; };
// Specialization for type T2==T1 producing true:
template <>
struct same_of<T1> { static const bool value = true; };
};
Defining a macro allows to use it easily:
#define is_type_same(T1,T2) (is_type<T1>::same_of<T2>::value)
as follows:
template<class R1, class R2>
bool operator==(Manager<R1> m1, Manager<R2> m2) {
return is_type_same(R1,R2) && m1.internal_field == m2.internal_field;
}