[英]Type trait test if template parameter is some instantiation of another template
Suppose in the following code the intention is to allow T
in Bar<T>
to be a Foo<U>
for any U
. 假定在下面的代码的意图是允许
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 {
// ...
};
Is there something I replace /*T is Foo<U> for any U*/
with? 我是否可以用
/*T is Foo<U> for any U*/
替换/*T is Foo<U> for any U*/
?
You can write a general trait to match for any specialization: 您可以编写一般特征以匹配任何专业化:
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 { };
Which in your specific case would be: 在您的特定情况下是:
is_specialization_of<T, Foo>::value // <== T is some kind of Foo
You can create a traits for that: 您可以为此创建一个特征:
template <typename T>
struct is_foo : std::false_type {};
template <typename T>
struct is_foo<Foo<T>> : std::true_type {};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.