Samvel
Samvel

Reputation: 127

Templates and explicit specializations

I have a template

template<class T>
T maxn(T *, int);

and explicit specialization for char*

template<> char* maxn(char**, int);

And I want to add const,

T maxn(const T *, int);

but after adding const in explicit specialization there is an error.

template<>char* maxn<char*>(const char**, int);

Why? Who can explain to me?

P.S. Sorry for my English.))

Upvotes: 1

Views: 109

Answers (2)

songyuanyao
songyuanyao

Reputation: 172864

Given the parameter type const T *, const is qualified on T. Then for char* (the pointer to char) it should be char* const (const pointer to char), but not const char* (non-const pointer to const char).

template<class T>
T maxn(const T *, int);

template<>
char* maxn<char*>(char* const *, int);
//                      ~~~~~

Upvotes: 2

Dmitry Gordon
Dmitry Gordon

Reputation: 2324

You should instantiate the method for const char*:

template<> const char* maxn<const char*>(const char**, int);

template<>char* maxn<char*>(const char**, int); doesn't correspond to

template<class T>
T maxn(T *, int);

signature, so you can't just add const to one parameter.

Upvotes: 2

Related Questions