[英]Template template functions and parameters deduction
I've got a problem with template templates and parameters deduction. 我遇到了模板模板和参数扣除的问题。 Here's the code:
这是代码:
template<typename U, template<typename> class T>
void test(T<U>&& t)
{
...
}
I expected this to accept either lvalues and rvalues, but only works with rvalues. 我希望这可以接受左值和左值,但只适用于右值。 The collapsing rule "T& && = T&" doesn't apply in this case?
折叠规则“T &&& = T&”在这种情况下不适用?
Naturally I could declare the lvalue reference function too, but makes the code less readable. 当然,我也可以声明左值引用函数,但是使代码的可读性降低。
If you're asking why I need this is to use a static_assert to check if T is a particular class. 如果你问我为什么需要这个就是使用static_assert来检查T是否是一个特定的类。 If there's a simpler way to do so I'll be happy to change my code, but I'd like to know if template templates are usable in this way.
如果有一种更简单的方法,我会很乐意改变我的代码,但我想知道模板模板是否可以这种方式使用。
Thanks 谢谢
Unlike typename T
, which can be deduced to be a reference type, template<typename> class T
can only ever be deduced to be a class template, so T<U>
is always deduced to an object type. 与
typename T
(可以推导为引用类型)不同, template<typename> class T
只能被推导为类模板,因此T<U>
总是推导为对象类型。
You can write your function templated on T
then unpack the template type in the static_assert
: 您可以在
T
上编写模板化的函数,然后在static_assert
解压缩模板类型:
template<typename T> struct is_particular_class: std::false_type {};
template<typename U> struct is_particular_class<ParticularClass<U>>: std::true_type {};
template<typename T> void test(T &&) {
static_assert(is_particular_class<std::remove_reference<T>::type>::value, "!");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.