Search code examples
c++classtemplatestypestypeid

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?


Solution

  • 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)>()) {...}