如果模板参数是另一个模板的某个实例,则类型特征测试

Type trait test if template parameter is some instantiation of another template

假设在下面的代码中,目的是让 Bar<T> 中的 T 成为任何 U.

Foo<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>
struct is_foo : std::false_type {};

template <typename T>
struct is_foo<Foo<T>> : std::true_type {};

您可以编写一个通用特征来匹配任何专业:

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