提問:
template <typename T> void fun(T num){};
如果調用
fun(double(1.23));
fun(int(1));
編譯器會實列化fun(double num)和fun(int num)
但是我想問得是如果調用兩次fun(int)
fun(int(1));
fun(int(2));
編譯器會實列兩個fun(int),還是一個啊????疑惑至極。
求好心人解答。
[cpp]
#include <iostream>
using namespace std;
template <typename T>
void fun(T num)
{
static int cnt = 0;
cnt++;
cout << "cnt = " << cnt << "\t" << "num = " << num << endl;
}
int main()
{
int a = 1;
fun(a);
int b = 2;
fun(b);
double c = 2.23;
fun(c);
return 0;
}
#include <iostream>
using namespace std;
template <typename T>
void fun(T num)
{
static int cnt = 0;
cnt++;
cout << "cnt = " << cnt << "\t" << "num = " << num << endl;
}
int main()
{
int a = 1;
fun(a);
int b = 2;
fun(b);
double c = 2.23;
fun(c);
return 0;
}
結果為:
cnt = 1 num = 1
cnt = 2 num = 2
cnt = 1 num = 2.23
結論:只有一次