Tom
Tom

Reputation: 1301

Is it possible to check if a object is equal to a template class without specifying the template type?

Suppose I have the class:

template<typename T>
class ChartData {
public:
...

Now I want to check if the object value is a ChartData object:

if (value.type() == typeid(ChartData*))

However this causes the error

argument list for class template is missing

So the compiler is expecting me to put a type at ChartData* however in this condition I'm not interested in the type - I just want to know if the object is a instance of a ChartData object.

Is this possible? If so, how?

Upvotes: 0

Views: 756

Answers (2)

n314159
n314159

Reputation: 5095

You can use template meta programming

#include <type_traits>

template<class, template<class...> class>
struct is_specialization : std::false_type {};

template<template<class...> class temp, class... tempargs>
struct is_specialization<temp<tempargs...>, temp> : std::true_type {};

template<class>
struct dummy {};
int main () {
    dummy<int> d;
    static_assert(is_specialization<decltype (d), dummy>::value);
}

This works for all templates with only type template arguments. If you have mixed type and non-type this is not really doable in a general way but you can of course write the above for one specific template and the fitting arguments.

Upvotes: 1

Igor Tandetnik
Igor Tandetnik

Reputation: 52621

Something along these lines:

template <typename T>
struct IsChartData : public std::false_type {};

template <typename T>
struct IsChartData<ChartData<T>> : public std::true_type {};

if (IsChartData<decltype(value)>()) {...}

Upvotes: 1

Related Questions