function template explicit instantiation

As an exercise on function templates. Xcode complains about both explicit template and the template. Can anyone help please!!

Thanks in advance.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 template void Max(char x, char y);

template <> const char *Max(const char *x, const char *y){
    std::cout << "Max <const char>()" << std::endl;
    return strcmp(x, y) > 0 ? x : y;
    
}


in main

    const char *b{ "B" };
    const char *a{ "A" };
    std::cout << Max(*a, *b) << std::endl;

Note that you don't have a templated function, Max. One returns nothing. One returns something.

You have a syntax error in the first line, so the explicit specialization of the non-existent templated function is, of course, something for the compiler to complain about if it happens to get that far.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <cstring>

// templated function:
template<typename T>
T Max(T a, T b) { return a > b ? a : b; }

// explicit specialization of templated function:
template <>
const char* Max(const char* a, const char* b) {
    return std::strcmp(a, b) > 0 ? a : b;
}

int main() {
    const char* b = "b";
    const char* a = "a";
    std::cout << Max(a, b) << '\n';
}


[edit: words]
Last edited on
Topic archived. No new replies allowed.