【深入理解C++】函数模板的自动类型推导

1.指针类型

#include <iostream>
using namespace std;

template <typename T>
void myfunc(T* val)
{
	cout << val << endl;
	return;
}

int main()
{
	int i = 180;
	const int* pi = &i;

	myfunc(&i); // T为int类型,val为int*类型
	myfunc(pi); // T为const int类型,val为const int*类型

    return 0;
}
#include <iostream>
using namespace std;

template <typename T>
void myfunc(const T* val)
{
	cout << val << endl;
	return;
}

int main()
{
	int i = 180;
	const int* pi = &i;

	myfunc(&i); // T为int类型,val为const int*类型
	myfunc(pi); // T为int类型,val为const int*类型

    return 0;
}

2.引用类型

#include <iostream>
using namespace std;

template <typename T>
void myfunc(T& val)
{
	cout << val << endl;
	return;
}

int main()
{
	myfunc(120); // 报错

	int i = 180;
	const int j = i;
	const int& k = i;

	myfunc(i); // T为int类型,val为int&类型
	myfunc(j); // T为const int类型,val为const int&类型
	myfunc(k); // T为const int类型,val为const int&类型

	return 0;
}
#include <iostream>
using namespace std;

template <typename T>
void myfunc(const T& val)
{
	cout << val << endl;
	return;
}

int main()
{
	int i = 180;
	const int j = i;
	const int& k = i;

	myfunc(i); // T为int类型,val为const int&类型
	myfunc(j); // T为int类型,val为const int&类型
	myfunc(k); // T为int类型,val为const int&类型

    return 0;
}

3.万能引用

#include <iostream>
using namespace std;

template <typename T>
void myfunc(T&& val)    // 万能引用
{
	cout << val << endl;
	return;
}

int main()
{
	myfunc(120); // 120是右值,T为int类型,val为int&&类型

	int i = 180;
	const int j = i;
	const int& k = i;

	myfunc(i); // i是左值,T为int&类型,val为int&类型
	myfunc(j); // j是左值,T为const int&类型,val为const int&类型
	myfunc(k); // k是左值,T为const int&类型,val为const int&类型

	return 0;
}

4.传值方式

#include <iostream>
using namespace std;

template <typename T>
void myfunc(T val)
{
	cout << val << endl;
	return;
}

int main()
{
	int i = 180;
	const int j = i;
	const int& k = i;

	myfunc(i); // T为int类型,val为int类型
	myfunc(j); // T为int类型,val为int类型,const属性没有传递进去
	myfunc(k); // T为int类型,val为int类型,const属性没有传递进去

	return 0;
}
#include <iostream>
using namespace std;

template <typename T>
void myfunc(T val)
{
	cout << val << endl;
	return;
}

int main()
{
	char str[] = "hello world!";
	const char* const p = str;
	myfunc(p); // T为const char*类型,val为const char*类型,第一个const保留了,第二个const没有了

	return 0;
}

5.数组做实参

#include <iostream>
using namespace std;

template <typename T>
void myfunc(T val)
{
	cout << val << endl;
	return;
}

int main()
{
	const char str[] = "hello world!";
	myfunc(str); // T为const char*类型,val为const char*类型

	return 0;
}
#include <iostream>
using namespace std;

template <typename T>
void myfunc(T& val)
{
	cout << val << endl;
	return;
}

int main()
{
	const char str[] = "I Love China!";
	myfunc(str); // T为const char [14]类型,val为const char (&)[14]类型

	return 0;
}
#include <iostream>
using namespace std;

template <typename T, unsigned LEN>
void myfunc(T (&val)[LEN])
{
	cout << LEN << endl; // 14
	return;
}

int main()
{
	const char str[] = "I Love China!";
	myfunc(str); // T为const char类型,val为const char (&)[14]类型

	return 0;
}

6.函数名做实参

#include <iostream>
using namespace std;

template <typename T>
void myfunc(T val)
{
	cout << val << endl;
	return;
}

void test() {}

int main()
{
	myfunc(test); // T为void (__cdecl*)(void)类型,val为void (__cdecl*)(void)类型

	return 0;
}
#include <iostream>
using namespace std;

template <typename T>
void myfunc(T& val)
{
	cout << val << endl;
	return;
}

void test() {}

int main()
{
	myfunc(test); // T为void __cdecl(void)类型,val为void (__cdecl&)(void)类型

	return 0;
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值