4

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?

Jarod42
  • 203,559
  • 14
  • 181
  • 302
Zebrafish
  • 11,682
  • 3
  • 43
  • 119
  • Since C++17, you can do `` instead of ``. That might simplify some things. Although it depends on the exact use case. You might have `` which is not the same as ``. – Indiana Kernick Apr 07 '21 at 01:36

1 Answers1

4

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

songyuanyao
  • 169,198
  • 16
  • 310
  • 405
  • Is that still a specialization of template struct IsAnimalOfAnyType { } even though it has three template arguments instead of one? Is that full or partial? – Zebrafish Apr 07 '21 at 01:21
  • 1
    @Zebrafish It's partial specialization. For full specialization it would be sth like `template <> struct IsAnimalOfAnyType<...> { ... };`. – songyuanyao Apr 07 '21 at 01:22