将调用转发到成员方法的模板
Template forwarding calls to a member's method
考虑以下内容:
template <typename type> class my_wrapper
{
type _;
template <typename... types, typename std :: enable_if <has_my_method_callable_with_the_following_types <type, types...> :: value> :: type * = nullptr> void my_method(types... items)
{
_.my_method(items...);
}
};
其中,正如您所想象的,has_my_method_callable_with_the_following_types
是某种SFINAE结构,它允许我确定type
是否有一个可以用这些类型调用的方法。
正如您很容易看到的,上面的示例基本上将所有对my_method
的调用向下转发到_
。嗯几乎所有。如果我这样做会发生什么:
class my_type
{
void my_method(int & x)
{
x++;
}
};
my_wrapper <my_type> my_wrapped;
int x;
my_wrapped.my_method(x);
显然,上面的方法不起作用,因为x
将通过复制传递给函数,而my_type :: my_method
通过引用接受它。所以我想知道:有没有办法解决这个问题?我当然可以:
template <typename... types, typename std :: enable_if <has_my_method_callable_with_the_following_types <type, types & ...> :: value> :: type * = nullptr> void my_method(types & ... items)
{
_.my_method(items...);
}
但对称地说,当我传递int
文字时,我会遇到问题,比如说,我不能通过引用来获得这些文字,但一些my_type :: my_method(int x)
完全可以接受这些文字。
我该如何解决这个问题?我想将所有到my_wrapper <type> :: my_method
的呼叫无缝地转发到type :: my_method
。
海盗小心:我不能使用继承,所以请不要建议!:(
这正是为引入完美转发和转发参考的原因
template <
typename... types,
typename std :: enable_if <has_my_method_callable_with_the_following_types <type, types...> :: value> :: type * = nullptr
> void my_method(types&&... items)
{
_.my_method(std::forward<types>(items)...);
}
工作原理:
该语言中有一条特殊规则,即当推导T&&
结构中的T
时,并且用于推导的自变量是类型为U
的左值,则T
被推导为U&
而不是U
。
净效应是,当一个参数是转发引用(对于推导的T
为T&&
(时,它要么是左值引用(如果该参数是左值(,要么是右值引用(若该参数是右值(。std::forward<T>
随后将根据需要将其强制转换回左值或右值。
相关文章:
- C++:在派生类成员上调用基类方法
- 从类C++外部调用指向成员方法的成员指针
- 在调用基类之前需要在成员上调用方法
- 简化静态成员函数调用的方法
- 如何检查是否调用了成员方法
- 对集合调用成员方法
- 从成员对象调用方法
- 我无法在班级中获得MED股票来调用成员方法
- 调用成员方法时会丢失此指针
- 动态调用成员方法c++
- 从另一个方法调用成员方法指针
- 以下4种样式调用其他成员方法有什么区别?
- 在C++中为成员对象调用受保护的方法
- 如何使用虚拟表正确调用成员方法
- 从另一个方法调用非静态成员方法
- 是否有可能在C++中的静态成员方法内部调用非静态成员方法
- 从 const 方法调用成员上的非常量方法
- 如何调用pass模板成员方法作为参数
- 函数的部分模板专用化,该函子调用传递给它的对象的成员方法
- 导致初始化的类成员变量在方法调用中变为(或看起来)未初始化的原因