1. 程式人生 > >C++當函式模板遇上函式過載

C++當函式模板遇上函式過載

//#include "stdafx.h"
#include <iostream>

using namespace std;

void func(int a, int b)
{
    cout << "普通函式:" << a << "\t" << b << endl;
}

template <typename T>
void func(T a, T b)
{
    cout << "模板函式:" << a << "\t" << b << endl;
}

int
main() { func(1, 2); //普通函式和模板函式都適合時,優先執行模板函式 func('a', 3); func(1.2, 1.4); //隱式呼叫 while模板更適合 func <> ('a', 't'); //顯式呼叫 cout << endl; system("pause"); return 0; }