#include <iostream> #include <cstring> using namespace std; template <typename T> T maxn(const T[], int); template <> const char * maxn(const char[], int); int main() { int arr1[]{ 3, 321, -41, 421, 4, 0 }; double arr2[]{ 42.42, -421.005, 0, 123.555 }; const char *arr3[]{ "ewqeeq", "321", "ew", "qwe", " " }; cout << maxn(arr1, 6) << endl; cout << maxn(arr2, 4) << endl; cout << maxn(arr2, 5) << endl; cin.get(); return 0; } template <typename T> T maxn(const T arr[], int n) { T thebiggest = *arr; for (int i = 1; i < n; i++) { if (*(arr + i) > thebiggest) { thebiggest = *(arr + i); } } return thebiggest; } template <> const char * maxn(const char arr[], int) { int long1 = strlen(*arr); const char * thelongest; for (int i = 0; i < n; i++) { if (strlen(*(arr + i) > long1) { long1 = strlen(*(arr + i); thelongest = (arr + i) } } return thelongest; } введите сюда код 

In general, the task is as follows: the function must receive an array of numbers and its length and return the greatest if the function receives an array of pointers to char, it must return a pointer to the longest string. I coped with the first part of the task, however, the second is not given to me in any way.
MS VS simply does not allow me to declare an explicit template specialization for the case of transferring the function to a pointer array, writes:

explicit specialization; "const char * maxn (const char [], int)" is not a specialization of the template function

.
(And one more sub-question: why can I not declare an array of pointers to char not constant?)
PS in the task it is indicated that I should use the specialization, and not the function overloading.

  • one
    Sealed. They said they needed an array of pointers to char . Not const char[] , but const char *[] . (Still, not "specification", but "specialization".) - HolyBlackCat
  • "why I can not declare an array of pointers to char not constant" It is not a constant itself, but pointers to constants in it. Because you initialize it with string literals, that is, const char [] s. The compiler does not just remove const so that you do not accidentally change something. - HolyBlackCat
  • one
    Thanks for the second comment. As for the first, I want to pass the function a pointer to an array of pointers. Oh yes, I will then, as I understand it, apply double dereferencing to extract the string literal itself, when I find its length, I already see an error in the code. But the problem is that MS VS doesn't even allow me to create this function specialization, even an empty one. Just writes "explicit specialization;" const char * maxn (const char [], int) "is not a specialization of the template function" Why not? I do not understand - Zondic
  • Do not consider it an insult, but you are clearly swimming in the pros, I would even say drowning. Are you sure that you need to study C ++ with such a complex topic as templates? It will only be more difficult. - RiotBr3aker
  • Hah, I do not know, like systematically moving through the book. Here is the second chapter on functions, and it discusses templates for functions. And in the tasks after, in fact, it is. I didn’t immediately start to study them, I went to them, sort of, systematically ... - Zondic

2 answers 2

For what specific value of the template parameter T are you trying to declare a specialization?

For example, if you wanted to declare a specialization for T == char , then after the substitution, the specialization would take the form

 template <> char maxn(const char[], int); 

But I do not see any value of type T for which the specialization of your template could take the form

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

This is what the compiler tells you.

    In the same place const initially stood, it is necessary const char *const []

     #include <iostream> #include <cstring> using namespace std; template <typename T> T maxn(const T[], int); template <> const char * maxn (const char *const[], int); int main() { int arr1[]{ 3, 321, -41, 421, 4, 0 }; double arr2[]{ 42.42, -421.005, 0, 123.555 }; const char *arr3[]{ "ewqeeq", "321", "ew", "qwe", " " }; cout << maxn(arr1, 6) << endl; cout << maxn(arr2, 4) << endl; cout << maxn(arr3, 5) << endl; cin.get(); return 0; } template <typename T> T maxn(const T arr[], int n) { T thebiggest = *arr; for (int i = 1; i < n; i++) { if (*(arr + i) > thebiggest) { thebiggest = *(arr + i); } } return thebiggest; } template <> const char * maxn(const char * const arr[], int n) { unsigned int long1 = strlen(arr[0]); const char * thelongest = arr[0]; for (int i = 0; i < n; i++) { if (strlen(arr[i]) > long1) { long1 = strlen(arr[i]); thelongest = arr[i]; } } return thelongest; } 

    Here is the final solution to the problem, if someone is interested. The book "C ++ Programming Language" by S. Prath. 6th edition. Chapter 8, task 6.

    • Not. const here on the side of the cake. The main correction you made has nothing to do with const . The main fix is ​​the appearance of * in the function parameter declaration. - AnT
    • @AnT if you remove the second declaration const, the code will not compile - Zondic
    • Of course. But we are talking about the fact that in your original version there is no place to put the “second const” at all. There can be no "second const" due to the fact that there is not enough * . This is the main mistake. - AnT