C++11中std::forward的使用
std::forward argument: Returns an rvalue reference to arg if arg is not an lvalue reference; If arg is an lvalue reference, the function returns arg without modifying its type.
std::forward:This is a helper function to allow perfect forwarding of arguments taken as rvalue references to deduced types, preserving any potential move semantics involved.
std::forward<T>(u)有两个参数:T 与 u。当T为左值引用类型时,u将被转换为T类型的左值,否则u将被转换为T类型右值。如此定义std::forward是为了在使用右值引用参数的函数模板中解决参数的完美转发问题。
std::move是无条件的转为右值引用,而std::forward是有条件的转为右值引用,更准确的说叫做Perfect forwarding(完美转发),而std::forward里面蕴含着的条件则是Reference Collapsing(引用折叠)。
std::move不move任何东西。std::forward也不转发任何东西。在运行时,他们什么都不做。不产生可执行代码,一个比特的代码也不产生。
std::move和std::forward只是执行转换的函数(确切的说应该是函数模板)。std::move无条件的将它的参数转换成一个右值,而std::forward当特定的条件满足时,才会执行它的转换。
std::move表现为无条件的右值转换,就其本身而已,它不会移动任何东西。 std::forward仅当参数被右值绑定时,才会把参数转换为右值。 std::move和std::forward在运行时不做任何事情。
下面是从其他文章中copy的测试代码,详细内容介绍可以参考对应的reference:
#include "forward.hpp"
#include <utility>
#include <iostream>
#include <memory>
#include <string>
//////////////////////////////////////////////
// reference: http://en.cppreference.com/w/cpp/utility/forward
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 test_forward1()
{
auto p1 = make_unique1<A>(2); // rvalue
int i = 1;
auto p2 = make_unique1<A>(i); // lvalue
std::cout << "B\n";
auto t = make_unique<B>(2, i, 3);
return 0;
}
////////////////////////////////////////////////////////
// reference: http://www.cplusplus.com/reference/utility/forward/
// function with lvalue and rvalue reference overloads:
void overloaded(const int& x) { std::cout << "[lvalue]"; }
void overloaded(int&& x) { std::cout << "[rvalue]"; }
// function template taking rvalue reference to deduced type:
template <class T> void fn(T&& x) {
overloaded(x); // always an lvalue
overloaded(std::forward<T>(x)); // rvalue if argument is rvalue
}
int test_forward2()
{
int a;
std::cout << "calling fn with lvalue: ";
fn(a);
std::cout << '\n';
std::cout << "calling fn with rvalue: ";
fn(0);
std::cout << '\n';
return 0;
}
//////////////////////////////////////////////////////
// reference: http://stackoverflow.com/questions/8526598/how-does-stdforward-work
template<class T>
struct some_struct{
T _v;
template<class U>
some_struct(U&& v) : _v(static_cast<U&&>(v)) {} // perfect forwarding here
// std::forward is just syntactic sugar for this
};
int test_forward3()
{
/* remember the reference collapsing rules(引用折叠规则):
前者代表接受类型,后者代表进入类型,=>表示引用折叠之后的类型,即最后被推导决断的类型
TR R
T& &->T& // lvalue reference to cv TR -> lvalue reference to T
T& &&->T& // rvalue reference to cv TR -> TR (lvalue reference to T)
T&& &->T& // lvalue reference to cv TR -> lvalue reference to T
T&& &&->T&& // rvalue reference to cv TR -> TR (rvalue reference to T) */
some_struct<int> s1(5);
// in ctor: '5' is rvalue (int&&), so 'U' is deduced as 'int', giving 'int&&'
// ctor after deduction: 'some_struct(int&& v)' ('U' == 'int')
// with rvalue reference 'v' bound to rvalue '5'
// now we 'static_cast' 'v' to 'U&&', giving 'static_cast<int&&>(v)'
// this just turns 'v' back into an rvalue
// (named rvalue references, 'v' in this case, are lvalues)
// huzzah, we forwarded an rvalue to the constructor of '_v'!
// attention, real magic happens here
int i = 5;
some_struct<int> s2(i);
// in ctor: 'i' is an lvalue ('int&'), so 'U' is deduced as 'int&', giving 'int& &&'
// applying the reference collapsing rules yields 'int&' (& + && -> &)
// ctor after deduction and collapsing: 'some_struct(int& v)' ('U' == 'int&')
// with lvalue reference 'v' bound to lvalue 'i'
// now we 'static_cast' 'v' to 'U&&', giving 'static_cast<int& &&>(v)'
// after collapsing rules: 'static_cast<int&>(v)'
// this is a no-op, 'v' is already 'int&'
// huzzah, we forwarded an lvalue to the constructor of '_v'!
return 0;
}
////////////////////////////////////////////////////
// reference: https://oopscenities.net/2014/02/01/c11-perfect-forwarding/
void sum(int a, int b)
{
std::cout << a + b << std::endl;
}
void concat(const std::string& a, const std::string& b)
{
std::cout<< a + b << std::endl;
}
void successor(int a, int& b)
{
b = ++a;
}
template <typename PROC, typename A, typename B>
void invoke(PROC p, A&& a, B&& b)
{
p(std::forward<A>(a), std::forward<B>(b));
}
int test_forward4()
{
invoke(sum, 10, 20);
invoke(concat, "Hello", "world");
int s = 0;
invoke(successor, 10, s);
std::cout << s << std::endl;
return 0;
}
GitHub:https://github.com/fengbingchun/Messy_Test
C++11中std::forward的使用的更多相关文章
- C++11中std::forward的使用 (转)
std::forward argument: Returns an rvalue reference to arg if arg is not an lvalue reference; If arg ...
- C++11中std::move、std::forward、左右值引用、移动构造函数的测试
关于C++11新特性之std::move.std::forward.左右值引用网上资料已经很多了,我主要针对测试性能做一个测试,梳理一下这些逻辑,首先,左值比较熟悉,右值就是临时变量,意味着使用一次就 ...
- C++11中std::function的使用
class template std::function is a general-purpose polymorphic function wrapper. Instances of std::fu ...
- C++11中std::move的使用
std::move is used to indicate that an object t may be "moved from", i.e. allowing the effi ...
- C++11中std::bind的使用
std::bind: Each argument may either be bound to a value or be a placeholder: (1).If bound to a value ...
- C++11中std::unordered_map的使用
unordered map is an associative container that contains key-value pairs with unique keys. Search, in ...
- item 23: 理解std::move和std::forward
本文翻译自<effective modern C++>,由于水平有限,故无法保证翻译完全正确,欢迎指出错误.谢谢! 博客已经迁移到这里啦 根据std::move和std::forward不 ...
- 用C++11的std::async代替线程的创建
c++11中增加了线程,使得我们可以非常方便的创建线程,它的基本用法是这样的: void f(int n); std::thread t(f, n + 1); t.join(); 但是线程毕竟是属于比 ...
- C++11 使用 std::async创建异步程序
c++11中增加了线程,使得我们可以非常方便的创建线程,它的基本用法是这样的: void f(int n); std::thread t(f, n + 1); t.join(); 但是线程毕竟是属于比 ...
随机推荐
- HTTPS科普(转)
为什么需要https HTTP是明文传输的,也就意味着,介于发送端.接收端中间的任意节点都可以知道你们传输的内容是什么.这些节点可能是路由器.代理等. 举个最常见的例子,用户登陆.用户输入账号,密码, ...
- python解析HTML之:PyQuery库的介绍与使用
本篇大部分转载于https://www.jianshu.com/p/c07f7cd1b548 先放自已自己解析techweb一个网站图片的代码 from pyquery import PyQuery ...
- Android(java)学习笔记51:ScrollView用法
1. 理论部分 (1)ScrollView和HorizontalScrollView是为控件或者布局添加滚动条 (2)上述两个控件只能有一个孩子,但是它并不是传统意义上的容器 (3)上述两个控件可以互 ...
- softmax实现cifar10分类
将cifar10改成单一通道后,套用前面的softmax分类,分类率40%左右,想哭... .caret, .dropup > .btn > .caret { border-top-col ...
- bzoj 2434 [Noi2011]阿狸的打字机——AC自动机
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2434 dfs AC自动机,走过的点权值+1,回溯的时候权值-1:走到询问的 y 串的节点,看 ...
- 2018.12.24 Spring中的aop演示(也就是运用aop技术实现代理模式)
Aop的最大意义是:在不改变原来代码的前提下,也不对源代码做任何协议接口要求.而实现了类似插件的方式,来修改源代码,给源代码插入新的执行代码. 1.spring中的aop演示 aop:面向方面编程.不 ...
- TemplateSyntaxError at /article/list-article-titles/admin/
如图红圈所示,发现一个注释掉的{% if userinfo %}标签竟然可以影响后面的标签快,不能注释,需要完全删除才不会报错. 继续这类django在html模板中直接注释掉发生错误以及解决方案: ...
- 统计决策——贝叶斯决策理论(Bayesian Decision Theory)
(本文为原创学习笔记,主要参考<模式识别(第三版)>(张学工著,清华大学出版社出版)) 1.概念 将分类看做决策,进行贝叶斯决策时考虑各类的先验概率和类条件概率,也即后验概率.考虑先验概率 ...
- EntityFramework Code-First-------领域类配置之DataAnnotations
EF Code-First提供了一个可以用在领域类或其属性上的DataAnnotation特性集合,DataAnnotation特性会覆盖默认的EF约定. DataAnnotation存在于两个命名空 ...
- A Year in Computer Vision
A Year in Computer Vision http://themtank.org/