#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.
char. Notconst char[], butconst char *[]. (Still, not "specification", but "specialization".) - HolyBlackCatconst char []s. The compiler does not just removeconstso that you do not accidentally change something. - HolyBlackCat