I'm trying to do a specialization to a const function template, but i obtain this:
C:\proyectos3\Capitulo 8\EX6\main.cpp|11|error: template-id 'maxn<>' for 'const char* maxn(const char**, int)' does not match any template declaration|
If you must specialise the function template template <typename T> const T* maxn( const T* arr, int n );,
the specialisation must have the same signature as the base template, with type parameter T being substituted with the type in the specialisation.
For example, this is fine:
1 2 3 4 5 6 7 8 9 10 11
//TEMPLATE PROTOTYPE
template <typename T> const T* maxn( const T* arr, int n );
// SPECIALIZATION PROTOTYPE
// 'T' in the base template <=> 'const char*' in the specialisation
using pcchar = constchar* ;
template <> const pcchar* maxn( const pcchar* arr, int n);
// redeclaration of the specialisation: same as above
// 'T' in the base template <=> 'const char*' in the specialisation
template <> constchar* const* maxn( constchar* const* arr, int n );
In general, if you want to customise a function template, overload the function; do not specialise it.
This works intuitively: the overload participates in normal overload resolution.
For instance:
1 2 3 4 5
// function template
template <typename T> const T* maxn( const T* arr, int n );
// overload maxn (do not specialise)
constchar * maxn(constchar ** arr, int n);