问题描述
我昨天 (How to find out if a type is a templated type of any type?) 问了一个问题,当该参数是任何类型的模板化类时,如何检查特定模板参数。解决方案是这样的:
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; }
};
然而,这适用于单参数模板,但我正在尝试执行以下操作:
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
*/
据我所知,区别在于 struct Animal:
- 有多个模板参数 和
- 其中一个参数不是类型,而是整数
如何去做?
解决方法
您可以声明 Animal
为特化所需的所有模板参数。
template <typename T,T integer,typename U>
struct IsAnimalOfAnyType<Animal<T,integer,U>>
{
constexpr bool value() { return true; }
};