0

I was implementing a function to sort different containers containting different types. I have created template as such:

template<typename Container>
void mySort(Container& container)

And it works for all the purposes I need it to, however for an argument being char[][] I want to change the implementation, so I declared template specialization as such:

template <>
void mySort<char**>(char**& container)

But my program is still using the generic implementation. What is causing this behaviour, have I made mistakes in declaring the specialization?

3
  • Plz, show the container you used
    – asmmo
    Apr 10, 2020 at 19:11
  • 2
    @KonradP The type char ( * )[N] to which a two-dimensional array is implicitly converted and the type char ** are two different types. Apr 10, 2020 at 19:12
  • We can't take it on faith that the argument you're providing is, in fact, char**. Show the code. A proper minimal reproducible example will belay all the banter and drive exactly to your problem. It is highly likely you template specialization doesn't match what you think it does, but that is guesswork and soothsaying. Show the code.
    – WhozCraig
    Apr 10, 2020 at 19:19

1 Answer 1

1

For the char[][] version, you could provide

template <int N, int M>
void mySort (char (&container) [N][M]); // version 1

and the correct function-template will be instantiated, and called.

Given the other function-template

template<typename Container>
void mySort(Container& container) // version 2

you can use them like this

std::string s;
mySort(s); // calls version 1
char c[10][10] ;
mySort(c); // calls version 2

Note that there is no specialization going on here. These are separate function-templates entirely.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.