标签:util nta 一个 stat net remember plain repr 详细
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>
-
- 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);
- int i = 1;
- auto p2 = make_unique1<A>(i);
-
- std::cout << "B\n";
- auto t = make_unique<B>(2, i, 3);
-
- return 0;
- }
-
- void overloaded(const int& x) { std::cout << "[lvalue]"; }
- void overloaded(int&& x) { std::cout << "[rvalue]"; }
-
- template <class T> void fn(T&& x) {
- overloaded(x);
- overloaded(std::forward<T>(x));
- }
-
- 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;
- }
-
- template<class T>
- struct some_struct{
- T _v;
- template<class U>
- some_struct(U&& v) : _v(static_cast<U&&>(v)) {}
-
- };
-
- int test_forward3()
- {
-
-
- some_struct<int> s1(5);
-
-
-
-
-
-
-
-
-
- int i = 5;
- some_struct<int> s2(i);
-
-
-
-
-
-
-
-
-
- return 0;
- }
-
- 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的使用 (转)
标签:util nta 一个 stat net remember plain repr 详细
原文地址:http://www.cnblogs.com/wangbin/p/7509804.html