Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell if template type is an instance of a template class?

I have a function that takes a template type to determine a return value. Is there any way to tell at compile time if the template type is some instantiation of a template class?

Ex.

class First { /* ... */ };

template <typename T>
class Second { /* ... */ };

using MyType = boost::variant<First, Second<int>, Second<float>>;

template <typename SecondType>
auto func() -> MyType {
    static_assert(/* what goes here?? */, "func() expects Second type");
    SecondType obj;
    // ...
    return obj;
}

MyType obj = func<Second<int>>();

I know it is possible to get around this by doing

template <typename T>
auto func() -> MyType {
    static_assert(std::is_same<T, int>::value || std::is_same<T, float>::value,
                  "func template must be type int or float");

    Second<T> obj;
    // ...
    return obj;
}

MyType obj = func<int>();

I'm just curious in general if there is a way to test if a type is an instantiation of a template class? Because if MyType ends up having 6 Second instantiations, I don't want to have to test for all possible types.

like image 578
Cameron Rowe Avatar asked May 16 '17 22:05

Cameron Rowe


2 Answers

Here's an option:

#include <iostream>
#include <type_traits>
#include <string>

template <class, template <class> class>
struct is_instance : public std::false_type {};

template <class T, template <class> class U>
struct is_instance<U<T>, U> : public std::true_type {};

template <class>
class Second 
{};

int main()
{
    using A = Second<int>;
    using B = Second<std::string>;
    using C = float;
    std::cout << is_instance<A, Second>{} << '\n'; // prints 1
    std::cout << is_instance<B, Second>{} << '\n'; // prints 1
    std::cout << is_instance<C, Second>{} << '\n'; // prints 0
}

It's basically specializing the is_instance struct for types that are instantiations of a template.

like image 63
Fatih BAKIR Avatar answered Sep 20 '22 16:09

Fatih BAKIR


Another option, picking up on Henri's comment:

#include <iostream>
#include <type_traits>
#include <string>

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

template <class...Ts, template <class, class...> class U>
struct is_instance<U<Ts...>, U> : public std::true_type {};

template <class>
class Second 
{};

template <class, class, class>
class Third 
{};

int main()
{
    using A = Second<int>;
    using B = Second<std::string>;
    using C = float;
    using D = Third<std::string, int, void>;
    std::cout << is_instance<A, Second>{} << '\n'; // prints 1
    std::cout << is_instance<B, Second>{} << '\n'; // prints 1
    std::cout << is_instance<C, Second>{} << '\n'; // prints 0
    std::cout << is_instance<D, Third>{} << '\n'; // prints 1
}
like image 44
Richard Hodges Avatar answered Sep 23 '22 16:09

Richard Hodges