Defining a Function Template Specialization
When we specialize a function template, we must supply arguments for every template parameter in the original template. To indicate that we are specializing a template, we use the keyword template
followed by an empty pair of angle brackets (< >
). The empty brackets indicate that arguments will be supplied for all the template parameters of the original template:
// special version of compare to handle pointers to character arraystemplate <>int compare(const char* const &p1, const char* const &p2){ return strcmp(p1, p2);}
The hard part in understanding this specialization is the function parameter types. When we define a specialization, the function parameter type(s) must match the corresponding ...
Get C++ Primer, Fifth Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.