假设在下面的代码中,目的是允许T
中的Bar<T>
为任何Foo<U>
的U
。
template<typename U>
class Foo { };
template<typename T, typename = std::enable_if_t< /*T is Foo<U> for any U*/>>
class Bar {
// ...
};
有什么东西我取代/*T is Foo<U> for any U*/
?
您可以编写一般特征以匹配任何专业化:
template <typename T, template <typename...> class Z>
struct is_specialization_of : std::false_type { };
template <typename... Args, template <typename....> class Z>
struct is_specialization_of<Z<Args...>, Z> : std::true_type { };
在您的具体情况中,将是:
is_specialization_of<T, Foo>::value // <== T is some kind of Foo
您可以为此创建特征:
template <typename T>
struct is_foo : std::false_type {};
template <typename T>
struct is_foo<Foo<T>> : std::true_type {};