对转发函数std::forward(位于头文件<utility>中)的一些个人理解,可能会有谬误。
1 什么是forward
forward<T>(a),将a转换为类型T(a本身不变,返回一个转换后的变量),并根据T和a本来的特性(左值还是右值),决定返回值是左值还是右值。比如:
#include <iostream>
#include <utility>
using namespace std;
void func1(int& a) {
cout << "aaa" <<" "<<a<< endl;
}
void func1(int&& a) {
cout << "bbb" << " " << a << endl;
}
void func1(const int& a) {
cout << "ccc" << " " << a << endl;
}
int main() {
int a = 10;
func1(a) //aaa
func1(forward<int>(a)); //bbb,将a转化为右值的int
func1(forward<int>(10)); //bbb,将10转化为右值的int,右值不能被转化为左值
func1(forward<int&>(a)); //aaa,将a转化为左值的int
func1(forward<int&&>(a)); //bbb,将a转化为右值的int
func1(forward<const int&>(a)); //ccc,将a转化为左值的const int
func1(forward<const int>(20)); //ccc,将20转化为右值的const int,当没有其他合适的匹配函数时,右值可以匹配const T&
return 0;
}
2 forward有什么用
假设有这么一个情景,要使用一个封装函数创建其他类的实例,很自然就能想到以下的实现:
#include <iostream>
#include <utility>
using namespace std;
class test1 {
int a;
int b;
public:
test1() :a(0), b(0) {}
test1(const int& x, const int& y) :a(x), b(y) { cout << "consturctor1" << endl; }
test1(int&& x, int&& y) :a(x), b(y) { cout << "constructor2" << endl; }
};
template <typename T, typename... Args>
T makeClass(Args... args) {
return T(args...);
}
int main() {
test1 m = makeClass<test1>(10, 2); //constructor1
return 0;
}
但是,无论将函数定义为T makeClass(Args... args),T makeClass(Args&... args)还是T makeClass(Args&&... args),都只能调用到test1的复制构造函数而不能调用移动构造函数(也可以使用move,但那样又只能调用移动构造函数了)。原因在于C++模板参数推导规则,以下例子来自Template argument deduction - cppreference.com:
template<class... Types>
void f(Types&...);
void h(int x, float& y)
{
const int z = x;
f(x, y, z); // P = Types&..., A1 = x: deduced first member of Types... = int
// P = Types&..., A2 = y: deduced second member of Types... = float
// P = Types&..., A3 = z: deduced third member of Types... = const int
// calls f<int, float, const int>
}
template<class T>
int f(T&&); // P is an rvalue reference to cv-unqualified T (forwarding reference)
template<class T>
int g(const T&&); // P is an rvalue reference to cv-qualified T (not special)
int main()
{
int i;
int n1 = f(i); // argument is lvalue: calls f<int&>(int&) (special case)
int n2 = f(0); // argument is not lvalue: calls f<int>(int&&)
// int n3 = g(i); // error: deduces to g<int>(const int&&), which
// cannot bind an rvalue reference to an lvalue
}
1 当定义T makeClass(Args... args),调用test1 m = makeClass<test1>(10, 2)时,Args被推导为int,相当于定义了test1 makeClass(int, int),相当于按值传递创建了临时变量int tmp1= 10,tmp2 = 2然后调用test1(tmp1, tmp2);
2 当定义T makeClass(Args&... args),Args被推导为int,相当于定义了test1 makeClass(int&, int&),此时只能传入左值,test1自然也是调用的复制构造函数;
3 当定义T makeClass(Args&&... args),调用test1 m = makeClass<test1>(10, 2)时,Args被推导为int,相当于定义test1 makeClass(int&&, int&&),相当于按值传递创建了临时变量int&& tmp1= 10,tmp2 = 2然后调用test1(tmp1, tmp2),虽然传入makeClass的是右值,但通过临时变量复制,最终传入test1构造函数的是左值;调用test1 m = makeClass<test1>(a, b)时(a,b是某个左值),Args被推导为int&,相当于定义test1 makeClass(int&, int&),和2相同。
此时可以把代码做如下修改,使用forward,就可以根据传入makeClass的参数是左值还是右值,匹配相应的构造函数了:
template <typename T, typename... Args>
T makeClass(Args&&... args) {
//return T(args...);
return T(forward<Args>(args)...);
}
int main() {
test1 m = makeClass<test1>(10, 2); //constructor2
int c = 4, d = 5;
test1 n = makeClass<test1>(c, d); //constructor1
return 0;
}
究其原因,还是依靠上述模板参数推导规则(看上面int f(T&&)那个例子),推导出Args的类型,再通过Args和args本来的性质,使forward返回期望的类型(左值还是右值)。
以下例子来自std::forward - cppreference.com:
template<class T>
void wrapper(T&& arg)
{
// arg is always lvalue
foo(std::forward<T>(arg)); // Forward as lvalue or as rvalue, depending on T
}
- If a call to
wrapper()
passes an rvaluestd::string
, thenT
is deduced tostd::string
(notstd::string&
,const std::string&
, orstd::string&&
), andstd::forward
ensures that an rvalue reference is passed tofoo
. - If a call to
wrapper()
passes a const lvaluestd::string
, thenT
is deduced toconst std::string&
, andstd::forward
ensures that a const lvalue reference is passed tofoo
. - If a call to
wrapper()
passes a non-const lvaluestd::string
, thenT
is deduced tostd::string&
, andstd::forward
ensures that a non-const lvalue reference is passed tofoo
.
总之,左值和右值主要是一个编译器概念,目的主要是为了匹配不同特征标的函数(比如T&&和const T&)。将一个左值转换成右值,转换本身并不改变这个左值的任何特性,只是告诉编译器此时想要用那个接受右值的函数而已。