0

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?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Tom
  • 1,235
  • 9
  • 22
  • Not at runtime, no. But what about a base class for youtr template? – Quentin Feb 23 '20 at 17:38
  • 2
    There *are* no instances of `ChartData`, because `ChartData` is not a type – it's a template for creating types. The instantiations of the template are types, but they are as unrelated as if you had written them out by hand. – molbdnilo Feb 23 '20 at 17:44

2 Answers2

1

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)>()) {...}
Igor Tandetnik
  • 50,461
  • 4
  • 56
  • 85
1

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.

n314159
  • 4,990
  • 1
  • 5
  • 20
  • At the risk of blowing my own trumpet, I [challenge your assertion](https://stackoverflow.com/questions/55396786/check-if-class-is-a-template-specialization/55398444#55398444) ;) – Quentin Feb 23 '20 at 17:54
  • 1
    You are a sick genius. – n314159 Feb 23 '20 at 19:12