Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get template function type

I'm new in using templates in C++, I want to do different things depending on type used between < and >, so function<int>() and function<char>() won't do the same things. How can I achieve this?

template<typename T> T* function()
{
    if(/*T is int*/)
    {
        //...
    }
    if(/*T is char*/)
    {
        //...
    }
    return 0;
}
like image 579
Gintas_ Avatar asked Dec 21 '22 01:12

Gintas_


1 Answers

You want to use explicit specialization of your function template:

template<class T> T* function() {
};

template<> int* function<int>() {
    // your int* function code here
};

template<> char* function<char>() {
    // your char* function code here
};
like image 123
Paul Evans Avatar answered Dec 24 '22 01:12

Paul Evans