A
A
Ann As2015-10-20 22:30:21
Programming
Ann As, 2015-10-20 22:30:21

How to fix the program so that the specialization will work?

Specialization does not work in the program. The compiler produces no errors or warnings.
Problem from S. Prat's book "The C++ Programming Language" (2015, 6th edition). Problem 6, Chapter 8, p.428.

#include <iostream>
#include <cstring>

using namespace std;

template <typename T>
T maxn(const T * cArr, const int iSize);
template <>
char * maxn<char*>(char * const * cArr, const int iArrSize);

int main(void){

    int iTest = 0;
    double dTest = 0.0;

    int iArr[6] = {1, 2, 3, 7, 5, 6};
        int iArrSize = (sizeof(iArr))/(sizeof(iArr[0]));
    double dArr[4] = {1.0, 2.0, 3.0, 4.0};
        int dArrSize = (sizeof(dArr))/(sizeof(dArr[0]));
    const char * cNames[5] = {"Mona", "Elzushka", "Leopold", "Burger", "Arny"};
        int iSizeOf = (sizeof(cNames)) / (sizeof(cNames[0]));


    iTest = maxn(iArr, iArrSize);
        cout << "Test maxn(int) = " << iTest << endl;
    dTest = maxn(dArr, dArrSize);
        cout << "Test maxn(double) = " << dTest << endl;
    const char * cTest = maxn(cNames, iSizeOf);
        cout << "Test maxn(char) = " << cTest << endl;

    return 0;
}

template <typename T>
T maxn(const T* tArr, const int iSize)
{
    T tMax = tArr[0];

    for (int i = 0; i < iSize; i++)
    {
        if (tArr[i] > tMax){
            tMax = tArr[i];
        }
    }
    cout << "MAXN T " << endl;
    return tMax;
}
template <>
char * maxn<char*>(char * const * cArr, const int iArrSize)
{
    char * cMaxLen = &cArr[0][0];

    for (int i = 0; i < iArrSize; i++)
    {
        if (strlen(&cArr[i][0]) > strlen(cMaxLen)){
            cMaxLen = &cArr[i][0];
        }
    }
    cout << "MAXN C " << endl;
    return cMaxLen;
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Ann As, 2015-10-20
@snave

Problem solved.

#include <iostream>
#include <cstring>

using namespace std;

template <typename T>
T maxn(const T * cArr, const int iSize);
template <>
char const* maxn<char const*>(char const * const * cArr, const int iArrSize);

int main(void){

    int iTest = 0;
    double dTest = 0.0;

    int iArr[6] = {1, 2, 3, 7, 5, 6};
        int iArrSize = (sizeof(iArr))/(sizeof(iArr[0]));
    double dArr[4] = {1.0, 2.0, 3.0, 4.0};
        int dArrSize = (sizeof(dArr))/(sizeof(dArr[0]));
    const char * cNames[5] = {"Mona", "Elzushka", "Leopold", "Burger", "Arny"};
        int iSizeOf = (sizeof(cNames)) / (sizeof(cNames[0]));


    iTest = maxn(iArr, iArrSize);
        cout << "Test maxn(int) = " << iTest << endl;
    dTest = maxn(dArr, dArrSize);
        cout << "Test maxn(double) = " << dTest << endl;
    const char * cTest = maxn(cNames, iSizeOf);
        cout << "Test maxn(char) = " << cTest << endl;

    return 0;
}

template <typename T>
T maxn(const T* tArr, const int iSize)
{
    T tMax = tArr[0];

    for (int i = 0; i < iSize; i++)
    {
        if (tArr[i] > tMax){
            tMax = tArr[i];
        }
    }
    cout << "MAXN T " << endl;
    return tMax;
}

template <>
char const* maxn<char const*>(char const * const * cArr, const int iArrSize)
{
    const char * cMaxLen = &cArr[0][0];

    for (int i = 0; i < iArrSize; i++)
    {
        if (strlen(&cArr[i][0]) > strlen(cMaxLen)){
            cMaxLen = &cArr[i][0];
        }
    }
    cout << "MAXN C " << endl;
    return cMaxLen;
}

V
Vladimir Martyanov, 2015-10-20
@vilgeforce

Classic: what do you expect from the work of the program, what does the program produce when executed? Well, what did you find out in the debugger?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question