LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-26-2012, 01:31 PM   #1
newbie0101
Member
 
Registered: Nov 2011
Posts: 47

Rep: Reputation: Disabled
C++ specialization function


hello, i am working on programming tasks form book but i am stuck at one task where i don't clearly understand what to do and have no idea how to do it. i've done only part of the problem.
here's question(sorry for my engish, it's not my native lang.):
Code:
This part i've done:
write template of function maxn() that takes two arguments,
first is array of type T
second is an integer representing number of items in the array.
the function returns its biggest value.
Use the template in the program that uses the function template with array of six ints and four doubles.

At this point i am stuck:
the program should contain specialization that takes two args.
first: array of pointers to char
second: number of pointers.
it should return address of the longest string.
test the specialization with array of five strings pointers
here's my code:
Code:
#include <iostream>

using namespace std;

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

void maxn<char>(char * arr[], int n);

int main()
{
    int integers[6] = {1,3,6,4,0,2};
    double dble[4] = {193.69, 207.02, 0.09, 20.1};
    
    cout << "Max value from int is: ";
    cout << maxn(integers, 6) << endl;
    cout << "Max value from double is: ";
    cout << maxn(dble, 4);
}

template <typename T>
T maxn(T arr[], int n)
{
    T max = arr[0];
    for (int i = 1; i < n; i++)
        if (arr[i] > max)
            max = arr[i];
    return max;
}
can you show me a way how to do it ?
 
Old 05-26-2012, 01:43 PM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
the template has comparison operator, so arr[i] > max will call the comparator of the specific type. Using strings the comparator is working based on the alphabetical order. You need the longest one, so you need to modify how this comparator should work.
 
Old 05-27-2012, 07:38 AM   #3
newbie0101
Member
 
Registered: Nov 2011
Posts: 47

Original Poster
Rep: Reputation: Disabled
i know how to figure out which string is the longest one but i don't know what the declaration should look like. can you show me one ?
you can see i've tried one but i am not sure if this is correct:
Code:
void maxn<char>(char * arr[], int n);
 
Old 05-27-2012, 02:50 PM   #4
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
Hi, how about something like

Code:
template <> void maxn < char > (char * arr[], int n);
That's a template specialization. Alternatively, you can try

Code:
void maxn(char * arr[], int n);
That's a non-template function. Non template functions are preferred to templates of the same name.

Last edited by millgates; 05-27-2012 at 02:57 PM.
 
Old 05-28-2012, 04:59 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
I do not really understand:
Code:
...
int main()
{
    int integers[6] = {1,3,6,4,0,2};
    double dble[4] = {193.69, 207.02, 0.09, 20.1};
    string strings[5] = { "asdf", "qwer", "retyhrw", "3q4trc", "34rcq" };
    
    cout << "Max value from int is: ";
    cout << maxn(integers, 6) << endl;
    cout << "Max value from double is: ";
    cout << maxn(dble, 4) << endl;
    cout << "Max value from strings is: ";
    cout << maxn(strings, 5) << endl;
}
this code works well with strings using the same maxn, just it will return the last one not the longest one.
The problem is the comparison operator, you need to override it.
So create a new class: mystring:
Code:
class mystring: public string {
    public: mystring(const char a[]): string(a) {};
    friend bool operator> (mystring &s1, mystring &s2);
};

bool operator> (mystring &s1, mystring &s2) {return s1.length()>s2.length(); };
and use this one instead of the default string class.

(the line: void maxn<char>(char * arr[], int n); not needed)
 
Old 05-28-2012, 06:46 AM   #6
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by newbie0101 View Post
the program should contain specialization
That assignment has poor wording, because you can't specialize a template function. You overload it instead. You only specialize template classes.

Quote:
void maxn<char>(char * arr[], int n);
That is incorrect. You want to just overload the function.

Quote:
Originally Posted by millgates View Post
how about something like

Code:
template <> void maxn < char > (char * arr[], int n);
That's a template specialization.
Yes, that is a template specialization and I've even seen compilers accept that. But if I understand correctly, that is not valid C++. I think the standard says you can't fully specialize a templated function.

Quote:
Alternatively, you can try

Code:
void maxn(char * arr[], int n);
That's a non-template function. Non template functions are preferred to templates of the same name.
That is overloading the function, which so far as I understand is the only correct approach to this situation.

Last edited by johnsfine; 05-28-2012 at 07:12 AM.
 
Old 05-28-2012, 07:58 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
this is what you are looking for?

Code:
#include <iostream>
#include <string.h>

using namespace std;

template <typename T> T maxn(T arr[], int n);
template <> char * maxn(char * arr[], int n);

int main()
{
    int integers[6] = {1,3,6,4,0,2};
    double dble[4] = {193.69, 207.02, 0.09, 20.1};
    string strings[5] = { "asdfrtyurty", "qwer", "retyhrw", "3q4trc", "34rcq" };
    char * chars[5] = { "asdfrtyurty", "qwer", "retyhrw", "3q4trc", "34rcq" };
    
    cout << "Max value from int is: ";
    cout << maxn(integers, 6) << endl;
    cout << "Max value from double is: ";
    cout << maxn(dble, 4) << endl;
    cout << "Max value from strings is: ";
    cout << maxn(strings, 5) << endl;
    cout << "Max value from chars is: ";
    cout << maxn(chars, 5) << endl;
}

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


template <>
char * maxn<char *>(char *arr[], int n)
{
    unsigned max = strlen(arr[0]);
    char * r = arr[0];
    for (int i = 1; i < n; i++) {
        if (strlen(arr[i]) > max) {
            max = strlen(arr[i]);
            r = arr[i];
        }
    }
    return r;
}

_____________________________________
If someone helps you, or you approve of what's posted, click the "Add to Reputation" button, on the left of the post.
Happy with solution ... mark as SOLVED
(located in the "thread tools")
 
Old 05-28-2012, 10:28 AM   #8
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
Quote:
Originally Posted by johnsfine
Yes, that is a template specialization and I've even seen compilers accept that. But if I understand correctly, that is not valid C++. I think the standard says you can't fully specialize a templated function.
It compiles with g++ -Wall -Wextra -pedantic -ansi without errors. I do not have a copy of the standard (Is one available somewhere? Probably not.), so I can't tell for sure what the standard says about it. I never really used this, actually. But I have seen this used (I believe even in some tutorials). That does not mean it's correct, of course
Actually, isn't it the other way around: function templates can be fully specialized but not partially specialized?
 
Old 05-29-2012, 06:55 AM   #9
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
It seems the OP is MIA (perhaps because of the holiday weekend in the US?)

Here's my take:
Code:
#include <iostream>
#include <cstring>

template <typename T>
T& lexicocomp(T& lhs, T& rhs)
{
    return lhs > rhs ? lhs : rhs;
}

template <typename T>
T& charstrcomp(T& lhs, T& rhs)
{
    return strcmp(lhs, rhs) > 0 ? lhs : rhs;
}

template <typename T>
T& lengthcomp(T& lhs, T& rhs)
{
    return strlen(lhs) > strlen(rhs) ? lhs : rhs;
}

template <typename T>
T maxn(T arr[], int n, T& (*compare)(T& lhs, T& rhs) = lexicocomp)
{
    T max = arr[0];

    for (int i = 1; i < n; i++)
    {
        max = compare(arr[i], max);
    }

    return max;
}

int main()
{
    using namespace std;

    int integers[6]      = { 1, 3, 6, 4, 0, 2 };
    double dble[4]       = { 193.69, 207.02, 0.09, 20.1 };
    string strings[5]    = { "asdfrtyurty", "qwer", "retyhrw", "3q4trc", "34rcq" };
    const char* chars[5] = { "asdfrtyurty", "qwer", "retyhrw", "3q4trc", "34rcq" };

    cout << "Max value from int is: ";
    cout << maxn(integers, 6) << endl;

    cout << "Max value from double is: ";
    cout << maxn(dble, 4) << endl;

    cout << "Max value from strings is: ";
    cout << maxn(strings, 5) << endl;

    cout << "Max value from chars is: ";
    cout << maxn(chars, 5, charstrcomp) << endl;

    cout << "Max length from chars is: ";
    cout << maxn(chars, 5, lengthcomp) << endl;
}

Last edited by dwhitney67; 05-29-2012 at 06:59 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] C++ template specialization vendtagain Programming 5 08-31-2012 07:41 PM
Redhat specialization Binoy_redhat LinuxQuestions.org Member Intro 1 12-31-2011 05:04 AM
[SOLVED] Question Regarding C++ Template Specialization. BLXCryptor Programming 2 02-10-2011 12:23 PM
C++ operator= specialization neutrino17 Programming 2 04-23-2008 06:02 AM
$#^!% template specialization headache! The_Nerd Programming 3 04-27-2007 03:45 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:56 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration