C++11新特性之 std::forward(完美转发)(转)
我们也要时刻清醒,有时候右值会转为左值,左值会转为右值。 
(也许“转换”二字用的不是很准确)
如果我们要避免这种转换呢? 
我们需要一种方法能按照参数原来的类型转发到另一个函数中,这才完美,我们称之为完美转发。
std::forward就可以保存参数的左值或右值特性。
因为是这样描述的: 
When used according to the following recipe in a function template, forwards the argument to another function with the value category it had when passed to the calling function.
例子:
template<class T>
void wrapper(T&& arg)
{
foo(std::forward<T>(arg)); // Forward a single argument.
}
If a call to wrapper() passes an rvalue std::string, then T is deduced to std::string (not std::string&, const std::string&, or std::string&&), and std::forward ensures that an rvalue reference is passed to foo. 
If a call to wrapper() passes a const lvalue std::string, then T is deduced to const std::string&, and std::forward ensures that a const lvalue reference is passed to foo. 
If a call to wrapper() passes a non-const lvalue std::string, then T is deduced to std::string&, and std::forward ensures that a non-const lvalue reference is passed to foo.
看一段网站上的代码(http://en.cppreference.com/w/cpp/utility/forward):
#include <iostream>
#include <memory>
#include <utility>
#include <array> struct A {
A(int&& n) { std::cout << "rvalue overload, n=" << n << "\n"; }
A(int& n) { std::cout << "lvalue overload, n=" << n << "\n"; }
}; class B {
public:
template<class T1, class T2, class T3>
B(T1&& t1, T2&& t2, T3&& t3) :
a1_{std::forward<T1>(t1)},
a2_{std::forward<T2>(t2)},
a3_{std::forward<T3>(t3)}
{
} private:
A a1_, a2_, a3_;
}; template<class T, class U>
std::unique_ptr<T> make_unique1(U&& u)
{
return std::unique_ptr<T>(new T(std::forward<U>(u)));
} template<class T, class... U>
std::unique_ptr<T> make_unique(U&&... u)
{
return std::unique_ptr<T>(new T(std::forward<U>(u)...));
} int main()
{
auto p1 = make_unique1<A>(); // rvalue
int i = ;
auto p2 = make_unique1<A>(i); // lvalue std::cout << "B\n";
auto t = make_unique<B>(, i, );
}
//输出:
rvalue overload, n=
lvalue overload, n=
B
rvalue overload, n=
lvalue overload, n=
rvalue overload, n=
最后,记住: 
不管是T&&、左值引用、右值引用,std::forward都会按照原来的类型完美转发。
转自:https://blog.csdn.net/wangshubo1989/article/details/50485951
C++11新特性之 std::forward(完美转发)(转)的更多相关文章
- C++11新特性之 std::forward(完美转发)
		
我们也要时刻清醒,有时候右值会转为左值,左值会转为右值. (也许“转换”二字用的不是很准确) 如果我们要避免这种转换呢? 我们需要一种方法能按照参数原来的类型转发到另一个函数中,这才完美,我们称之为完 ...
 - C++11新特性 变参模板、完美转发(简述)
		
变参模板 (Variadic Template) - 使得 emplace 可以接受任意参数,这样就可以适用于任意对象的构建 完美转发 - 使得接收下来的参数 能够原样的传递给对象的构造函数,这带来另 ...
 - C++ 11新特性:std::future & std::shared_future) (转载)
		
上一讲<C++11 并发指南四(<future> 详解二 std::packaged_task 介绍)>主要介绍了 <future> 头文件中的 std::pack ...
 - C++ 11新特性:std bind 原理简单图解(转载)
		
本文解释了bind 是如何工作的.为了清晰,我对图中的语法作了一些简化(例如,省略函数调用操作符的参数类型),并且简化了 bind 的实现. bind 可以用来将用户提供的需要一个参数的函数转换成不需 ...
 - C++11新特性应用--实现延时求值(std::function和std::bind)
		
说是延时求值,注意还是想搞一搞std::function和std::bind. 之前博客<C++11新特性之std::function>注意是std::function怎样实现回调函数. ...
 - [转载] C++11新特性
		
C++11标准发布已有一段时间了, 维基百科上有对C++11新标准的变化和C++11新特性介绍的文章. 我是一名C++程序员,非常想了解一下C++11. 英文版的维基百科看起来非常费劲,而中文版维基百 ...
 - c++ 11 线程池---完全使用c++ 11新特性
		
前言: 目前网上的c++线程池资源多是使用老版本或者使用系统接口实现,使用c++ 11新特性的不多,最近研究了一下,实现一个简单版本,可实现任意任意参数函数的调用以及获得返回值. 0 前置知识 首先介 ...
 - C++ 11学习和掌握 ——《深入理解C++ 11:C++11新特性解析和应用》读书笔记(一)
		
因为偶然的机会,在图书馆看到<深入理解C++ 11:C++11新特性解析和应用>这本书,大致扫下,受益匪浅,就果断借出来,对于其中的部分内容进行详读并亲自编程测试相关代码,也就有了整理写出 ...
 - C++11新特性总结 (二)
		
1. 范围for语句 C++11 引入了一种更为简单的for语句,这种for语句可以很方便的遍历容器或其他序列的所有元素 vector<int> vec = {1,2,3,4,5,6}; ...
 
随机推荐
- #incldue<cctype>函数系列
			
#include <cctype>的函数 c++中应该是#include <cctype> c中应该是#include <ctype.h> 以下为字符函数库中常用的 ...
 - BZOJ1337: 最小圆覆盖
			
题目:求n个点的最小圆覆盖. 题解:最小圆覆盖,上模板.复杂度证明可以戳:这里 代码: #include<cstdio> #include<cstdlib> #include& ...
 - DBS-Tally book(记账本)
			
ylbtech-dbs:DBS-Tally book(记账本) -- =============================================-- 记账本-- 模仿小程序“记账e”业 ...
 - 7.6 服务远程暴露 - 注册服务到zookeeper
			
为了安全:服务启动的ip全部使用10.10.10.10 远程服务的暴露总体步骤: 将ref封装为invoker 将invoker转换为exporter 启动netty 注册服务到zookeeper 订 ...
 - 混合开发 Hybird Ionic Angular Cordova web 跨平台 MD
			
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
 - Java去掉Html标签的方法
			
content = content.replaceAll("\\&[a-zA-Z]{1,10};", "").replaceAll("< ...
 - 用Hadoop构建电影推荐系统
			
转自:http://blog.fens.me/hadoop-mapreduce-recommend/ Hadoop家族系列文章,主要介绍Hadoop家族产品,常用的项目包括Hadoop, Hive, ...
 - SQL-根据多个条件更新数据
			
根据多个条件更新数据 UPDATE sphwph SET BKXSHL=t2.BKXSHL FROM sphwph t1,sphwph_170420 t2 --(SELECT a.* FROM dbo ...
 - 文字编码和Unicode
			
文字编码和Unicode 说明文字: https://blog.csdn.net/fengzhishang2019/article/details/7859064 Java 程序: https://w ...
 - BCG在程序中的使用
			
首先你电脑上是安装有BCG的,详细安装方法就是先双击安装程序,之后编译当中的两个project.之后将其生成的.dll\.lib文件放入C++的include中这样就能够使用BCG的控件了. 1. 在 ...