Search code examples
c++templatespartial-specialization

How to check if a template argument is of a particular templated type (multiple type parameters)


I asked a question yesterday (How to find out if a type is a templated type of any type?) about how to check for a particular template argument when that argument is a templated class of any type. And the solution was something like this:

template <typename T>
struct Animal{};

template <typename T>
struct IsAnimalOfAnyType
{
    constexpr bool value() { return false; }
};

template <typename T>
struct IsAnimalOfAnyType<Animal<T>>
{
    constexpr bool value() { return true; }
};

However this works with single-argument templates, but I'm trying to do the following:

template <typename T, T integer, typename U>
struct Animal{};

template <typename T>
struct IsAnimalOfAnyType
{
    constexpr bool value() { return false; }
};


template <typename T> 
struct IsAnimalOfAnyType<Animal<T>>
{
    constexpr bool value() { return true; }
};

/* Animal needs to be Animal<T, integer, U>,
  but 'T', 'integer' and 'U' template arguments are not available here,
  and if I have these arguments here
  then IsAnimalOfAnyType is no longer a specialization and it won't compile
*/

As far as I understand it the difference is that struct Animal:

  1. Has multiple template arguments and
  2. One of the arguments is not a type, but an integer

How to go about doing this?


Solution

  • You can declare all the template parameters required by Animal for the specialization.

    template <typename T, T integer, typename U> 
    struct IsAnimalOfAnyType<Animal<T, integer, U>>
    {
        constexpr bool value() { return true; }
    };
    

    LIVE