0

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

5
Contributors
4
Replies
5
Views
7 Years
Discussion Span
Last Post by LordNemrod
0
#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
    }
}

Edited by vijayan121: n/a

0

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

EDIT:
Oops, too slow...

Edited by Fbody: n/a

0

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";
}
0

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.

Edited by LordNemrod: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.