引用折叠

一 引用折叠规则

    1、T& + & = T&

    2、T& + && = T&

    3、T&& + & = T&

    4、T或T&& + && = T&&

二 例子如下

#include "stdafx.h"
#include <iostream>
#include <typeinfo.h>
using namespace std;

typedef int &  lRef;
typedef int && rRef;

typedef const int &  lcRef;
typedef const int && rcRef;

void main()
{
	int a = 10;
	
	// 左值引用
	lRef  b  = a;  // b : int & 
	lRef  &c = a;  // c : int &, & and & = &

	// 右值引用
	rRef d = 10;   // d : int&&     
	rRef&& e = 10; // e : int&&, && and && = &&
	lRef&& f = a;  // f : int&,  &  and && = &
	rRef& g = a;   // g : int&,  && and  & = &

	//int a = 10;

	 左值引用
	//lcRef  b = a;   // b : const int & 
	//lcRef  &c = a;  // c : const int &, const & and & = const &

	 右值引用
	//rcRef d = 10;   // d : const int&&     
	//rcRef&& e = 10; // e : const int&&, const && and && = &&
	//lcRef&& f = a;  // f : const int&,  const &  and && = &
	//rcRef& g = a;   // g : const int&,  const && and  & = &
}

三 模板函数参数

#include "stdafx.h"
#include <iostream>
#include <typeinfo.h>
using namespace std;

template<typename T>
void FunTemple(T && param)
{

}

void main()
{
	// inParam : int         T : int&        param : int&
	// inParam : int&        T : int&        param : int& 
	// inParam : int&&(无名) T : int         param : int&&
	// inParam : int&&(有名) T : int&        param : int&
	// inParam : const int   T : const int&  param : const int&
	// inParam : const int&  T : const int&  param : const int&
	// inParam : const int&& T : const int&  param : const int&
	
	int inParam = 10;
	FunTemple(inParam);
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值