Sanka Darshana
Sanka Darshana

Reputation: 1421

Check whether if a class is a template class or not

Is there a way to detect whether a class is a template class or a simple class at compile time?

Eg:

class A
{
    public:
    void GetValue()
    {
        return 10 (compile time check? "": "+ 10"); // just an example
    }
};

class B : public A
{
};

template <class T>
class C : public A
{
};

Upvotes: 1

Views: 74

Answers (1)

Jarod42
Jarod42

Reputation: 217075

You may create a traits for that:

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

template <template <typename...> class C, typename ... Ts>
struct is_type_templated<C<Ts...>> : std::true_type {};

Live example

Note that it doesn't handle templated value (as std::array<T, N>).

Upvotes: 3

Related Questions