Stackoverflow template function specialization error

31 views Asked by At

I'm practicing template function specialization code.

But I got an error 'function template "T smaller(const T&first, const T&second)" is not an entity that can be explicitly specialized'

following is code. How can I fix this code?

main.cpp

/*
 * 15.1.3 : Using function template
 * Specialization
 */
#include <cstring>
#include <iostream>
#include <string>
using namespace std;

// template function
template<typename T>
T smaller(const T& first, const T& second)
{
    if (first < second)
    {
        return first;
    }
    return second;
}
// specialization of template function
template<>
const char* smaller(const(const char*) & first, const(const char*) & second)
{
    if (strcmp(first, second) < 0)
    {
        return first;
    }
    return second;
}
int main()
{
    // calling template with two string objects
    string str1 = "Hello";
    string str2 = "Hi";
    cout << "Smaller(Hello,Hi) : " << smaller(str1, str2) << endl;
    // calling template function with two C-string objects
    const char* s1 = "Bye";
    const char* s2 = "Bye Bye";
    cout << "Smaller (Bye, Bye Bye) : " << smaller(s1, s2) << endl;
    return 0;
}

I want to solve error

0

There are 0 answers