In this post, I consider the problem of checking whether the type matches exactly the other, this is not exactly what is required, but it is simpler, and I hope that this helps to understand the template tricks used.
As in boost, specialized templates can be used for this task, in fact you can define a template structure containing operations on this type and use nested template structures for these 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 makes it easy to use:
#define is_type_same(T1,T2) (is_type<T1>::same_of<T2>::value)
in the following way:
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; }
Gabriele Lombardi Sep 11 '12 at 22:00 2012-09-11 22:00
source share