I'd like to implement something like same_type() function in C++11 like below (this is what I have tried so far but it can not cope with the use cases mentioned below). The function is to check if T
is of the same type of the n-th argument of Args
, currently n=0 should be enough for my requirement, although n=other meaningful value would be something better to have (not very important if not straightforward).
template<typename T, typename... Args>
struct MyClass
{
// check if T is of the same type of the n-th argument of Args
bool same_type() {
// currently, I only check the first argument
// but if possible, it would be more useful to extend
// this function to check the n-th argument
return std::is_same<T,typename std::tuple_element<0, std::tuple<Args...> >::type>;
}
};
I have already had a look at this answer, but it does not consider the following use cases.
Use cases:
1.use with references and const
qualifier:
MyClass<int,const int&> c;
// expect to see 1, type of int should match const int&, i.e. reference or const should NOT affect the result
std::cout<<c.same_type();
2.use with no argument supplied:
MyClass<int> c;
// expect to see 0 as there is no variadic argument provided to compare with int
std::cout<<c.same_type();