M
M
Madara3372020-10-18 18:04:47
C++ / C#
Madara337, 2020-10-18 18:04:47

An error occurred while creating an explicit specialization. Where is the mistake?

Doing exercise 6 of chapter 8 of the book "The C++ Programming Language" by Stephen Pratt

Write a template function maxn() that takes an
array of elements of type m and an integer representing the number
of elements in the array as an argument, and returns the element with the largest value. Test
it in a program that uses this pattern with an array of six
int values ​​and an array of four double values.
The program must also include a specialization that takes an array of pointers to char
as the first argument and a number of pointers as the second, and then
returns the address of the longest string. If there is more than one line
longest, the function should return the address of the first one. Test
the specialization on an array of five pointers to strings.

I get an error in VS19 :no instance of function template "maxn" matches the specified type.
What am I doing wrong?

#include <iostream>

template <typename T>
T maxn(T arr[], int arrSize);

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

int main()
{
  return 0;
}

template <typename T>
T maxn(T arr[], int arrSize)
{
  T max = arr[0];
  for (int i = 1; i < arrSize; i++)
    if (arr[i] > max)
      max = arr[i];

  return max;
}

template <> char* maxn(const char* arr[], int arrSize)
{
  char* line = arr[0];

  for (int i = 0; i < arrSize; i++)
    if (strlen(arr[i]) > strlen(line))
      line = arr[i];

  return line;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Shatunov, 2020-10-18
@Madara337

template <typename T>
T maxn(T arr[], int arrSize);

This is the general form of a function template. If you need to make an explicit template specialization, your explicit specialization must match this general template.
template <>
const char* maxn(const char* arr[], int arrSize);

Raw one-byte strings in C++ (subject to the null-termination rulechar* ) are determined by the type or const char*if the string is not going to be modified. Our function does not plan to modify strings, so explicit template specialization must work with const char*.
The type const char*must be substituted everywhere in place of the template parameter T.
Thus, an explicit template specialization for raw strings will fully match the general function template.
And now a little about your mistake.
template <> char* maxn(const char *arr[], int arrSize);

This declaration is not an explicit specialization of the above template. It is the definition of a function overload through an explicit specialization of some other undeclared pattern. This is because the result of the function char*is different from the function parameter const char*.
In general, this notation is ill-formed for a number of reasons, including the fact that overloading cannot be done by changing the result type of a function.
For educational purposes, I advise against omitting the explicit template specialization argument enumeration:
template <>
const char* maxn<const char*>(const char* arr[], int arrSize);

Thanks to this simple step, you will tell the compiler more information about what you want to say with your code and it will understand you better. As a result, the compiler will be able to correct you where you made a mistake when describing an explicit specialization.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question