std::forward的性质.
std::forward的性质.
SHIHUAMarryMe 发表于2年前
std::forward的性质.
  • 发表于 2年前
  • 阅读 61
  • 收藏 1
  • 点赞 1
  • 评论 0

【腾讯云】如何购买服务器最划算?>>>   

更新于:16/8/28

//针对左值的情况.
template<typename T>
constexpr T&& my_forward(typename std::remove_reference<T>::type& value)noexcept
{
	std::cout << "my-forward: &" << std::endl;
	
	return (static_cast<T&&>(value));
}


//针对右值的情况.
template<typename T>
constexpr T&& my_forward(typename std::remove_reference<T>::type&& value)noexcept
{
	std::cout << "my-forward: &&" << std::endl;

	return (static_cast<T&&>(value));
}

std::forward<T>用来完美转发,也就是说如果变量是个右值经过std::forward仍然是个右值,如果是个左值经过std::forward转发仍然是个左值.

demo:

#include <utility>
#include <tuple>
#include <iostream>
#include <type_traits>

template<class _Ty> inline
constexpr _Ty&& my_forward(
	typename std::remove_reference<_Ty>::type& _Arg) _NOEXCEPT
{	// forward an lvalue as either an lvalue or an rvalue

	std::cout << "forward: &" << std::endl;
	return (static_cast<_Ty&&>(_Arg));
}

template<class _Ty> inline
constexpr _Ty&& my_forward(
	typename std::remove_reference<_Ty>::type&& _Arg) _NOEXCEPT
{	// forward an rvalue as an rvalue
	static_assert(!std::is_lvalue_reference<_Ty>::value, "bad forward call");

	std::cout << "forward: &&" << std::endl;

	return (static_cast<_Ty&&>(_Arg));
}


template<typename T>
class Node {
private:
	T value;

public:
	~Node() = default;

	Node(T&& value_) :value(std::move(value_)) //注意这里我用了move!因为我们还有一个构造函数是const T&
	{ 
		//std::cout << std::boolalpha 
		//	      << typename std::is_same<decltype(value_), decltype(my_forward<T>(value_))>::value 
		//	      << typename std::is_same<T&&, decltype(value_)>::value
		//	      << typename std::is_same<T&&, decltype(my_forward<T>(value_))>::value
		//	      << std::endl;

		std::cout << "constructor: &&" << std::endl;
	}

	Node(const T& value_) :value(value_) { std::cout << "constructor: const&" << std::endl; }

	void print()const
	{
		std::cout << this->value << std::endl;
	}
};


template<typename T>
void wrapperFunction(T&& value)
{
	print(value);
}

template<typename T>
void wrapFunction(T&& value)
{
	print(std::forward<T>(value));
}

template<typename T>
void print(const T& value)
{
	std::cout << "T& : " << value << std::endl;
}

template<typename T>
void print(T&& value)
{
	std::cout << "T&& :" << value << std::endl;
}


template<typename T>
void packagePrint(T&& val)
{
	using type = typename std::remove_reference<std::remove_const<T>::type>::type;
	Node<type>(std::forward<T>(val));
}


template<typename Type, typename ...Types>
void packagePrint(Type&& arg, Types&&... args)
{
	using type = typename std::remove_reference<std::remove_const<Type>::type>::type;
	Node<type>(std::forward<Type>(arg));
	packagePrint(std::forward<Types>(args)...);
}

int main()
{
	//case 1:
	int number = 50;
	//wrapperFunction(number); //output: T& : 50;
	//wrapperFunction(50);     //output: T& : 50;

	//case 2:
	//wrapFunction(number);  //output: T& : 50;
	//wrapFunction(50);      //output: T&& :50;

	//case 3:
	packagePrint(20, number, std::basic_string<char>("shihuawoaini"));
	
	//注意这里!!!!!!!!!!!!
	int&& x = 10l;  //此时的x其实是一个右值引用的左值!!!!!所以我们必须用std::forward才能保持.
	int& y = x;
	//int&& z = x; //error!.

	return 0;
}

 

共有 人打赏支持
粉丝 11
博文 165
码字总数 138247
×
SHIHUAMarryMe
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: