How to use template to specialize a function with type char *?

0

I'm doing C ++ exercises and I've created a template for the function to return the highest value.

template<class Type>
Type maximo (const Type a, const Type b) {
    if (a > b) {
        return a;
    }
    return b;

Now the list of exercises asks me to specialize the function to work with the type char *, and gives the following model:

template <>
Tipo funcao<Tipo>(Tipo param1, Tipo param2, ...) { ... }

Type is the concrete type to be used in the specialization (int, double, char *, etc.).

  

Step 6: (Challenge 1) Specialize the max () function to handle char   * using the syntax above. Use the function strcmp (); you must include cstring. Make changes to main () to test these functions.   Compile and test next.

But I can not do it. My role was:

template <>
char * maximo<char *>(const char *a, const char *b) {
    if (strcmp(a ,b) < 0) {
        return a;
    }
    return b;
}
    
asked by anonymous 21.09.2017 / 04:34

1 answer

1

Note that in the template declaration, you did:

template<class Type>
Type maximo (const Type a, const Type b);

Now you want to specialize for char * . Then you should replace Type with char * , ie

template <>
char * maximo(char *a, char *b);

Note that you do not have const . If you want to replace with const char * , you have to stay:

template <>
const char * maximo(const char *a, const char *b);

that is, the return type must be const char *

    
21.09.2017 / 04:41