In summary, I would like to be able to check if the type of an object matches some type or if two objects match types.
The essence of what I'm trying to do is pasted below:
#include <vector>
#include <iostream>
#include <type_traits>
template< class T1, class T2 >
bool compare_types(T1, T2)
{
return std::is_same<T1, T2>::value;
}
class OBJ
{
};
int main(int argc, char *argv[])
{
OBJ a;
OBJ b;
int c;
std::cout<<"\n"<<compare_types(a, b)<<"\n"; // ok and returns true
//std::cout<<"\n"<<compare_types(c, int)<<"\n"; // compile error, ideally return true
//std::cout<<"\n"<<std::is_same<c, int>::value<<"\n"; // compile error, ideally return trues
//std::cout<<"\n"<<compare_types(a, OBJ)<<"\n"; // compile error, ideally return true
//std::cout<<"\n"<<std::is_same<a, OBJ>::value<<"\n"; // compile error, ideally return true
//std::cout<<"\n"<<compare_types(a, std::vector)<<"\n"; // compile error, ideally return false
//std::cout<<"\n"<<std::is_same<a, b>::value<<"\n"; // compile error, ideally return true
//std::cout<<"\n"<<std::is_same<a, c>::value<<"\n"; // compile error, ideally return false
//std::cout<<"\n"<<std::is_same<int, float>::value<<"\n"; // compile error, ideally return false
std::cout<<"\n"<<std::is_same<int, std::int32_t>::value<<"\n"; // ok and returns true
return 0;
}
intis not an instance. It's a type.So, you need to pass an instance of
intto yourcompare_types:And a type when you use
cas a template parameter. Fortunately,decltype(c)provides the type:Continue this pattern and your program would look like:
Demo
I don't know what you hope for with
std::vectorthere. It's not a type in itself. It's a template, so it'd need at least one template parameter (or in C++17 and later versions, something that makes it possible to deduceT).