網頁

2016年11月14日 星期一

Template part2

Explicitly Overloading Generic Functions with Normal Functions

#include <iostream>
using namespace std;
template <typename T> void whatYouGot (T x);
template <> void whatYouGot<int> (int x);
void whatYouGot (int x); //normal function
template <> void whatYouGot <int> (int x); //normal function進階
template <typename T> void whatYouGot (T x);

int main()
{
    whatYouGot (22.546); //generic function
    whatYouGot (23); //normal function and explicitly overloading generic function
    whatYouGot ("anil shetty"); //generic function
    return 0;
}

template <typename T> void whatYouGot (T x)
{
    cout << "inside whatYouGot generic function" << endl;
}

void whatYouGot (int x)
{
    cout << "inside whatYouGot normal function" << endl;
}

template <> void whatYouGot<int> (int x)
{
    cout << "inside whatYouGot explicitly overloading generic function" << endl;
}

沒有留言:

張貼留言