My templated function look as follows:

template <typename MatTypeOne, typename MatTypeTwo>
void funct(MatTypeOne& m1, MatTypeTwo& m2) {
     //here I would need to check whether m1 and m2 are of the same "type",
     //meaning that MatTypeOne is the same as MatTypeTwo
}

What is the way to check whether m1 and m2 belong to same/different types?
Thanks

Recommended Answers

All 4 Replies

#include <typeinfo>

template <typename MatTypeOne, typename MatTypeTwo>
void funct(MatTypeOne& m1, MatTypeTwo& m2)
{
    if( typeid(m1) == typeid(m2) )
    {
        // objects m1 and m2 are of the same (run-time) type
    }
}

I think the typeid operator and typeinfo header will be of interest to you.

EDIT:
Oops, too slow...

Another option:

template<class T1, class T2>
void foo(T1 a , T2 b){
  cout << "default\n";
}
template<typename T1>
void foo(T1 a, T1 b){
 cout << "specialized\n";
}

I believe the specialization alternative (although technically speaking function templates cannot be specialized, this is rather an overloading) is indubitably superior to the Runtime Type Identification.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.