#### Archived

This topic is now archived and is closed to further replies.

# templates??

This topic is 5098 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Can anyone figure this out? The two function calls at the bottom have been commented out, but when I remove the comments and try compiling this inside VC6, I get the following errors... error C2667: 'fn_max' : none of 2 overload have a best conversion error C2668: 'fn_max' : ambiguous call to overloaded function error C2667: 'fn_max' : none of 2 overload have a best conversion error C2668: 'fn_max' : ambiguous call to overloaded function Any suggestions would be most helpful. Thank you.
#include <iostream>
#include <cstring>
#include <string>
using namespace std;

// MAXIMUM OF TWO VALUES OF ANY TYPE

template <typename tType>
inline tType const& fn_max(tType const& a, tType const& b)
{
return  a < b  ?  b : a;
}

// MAXIMUM OF TWO POINTERS

template <typename tType>
inline tType* const& fn_max(tType* const& a, tType* const& b)
{
return  *a < *b  ?  b : a;
}

// MAXIMUM OF TWO C-STRINGS

inline char const* const& fn_max(char const* const& a, char const* const& b)
{
return strcmp(a, b) < 0  ?  b : a;
}

int main()
{
int a = 7, b = 42;
// COMPUTE MAX OF TWO VALUES OF TYPE int

fn_max(a, b);

string s = "hey";
string t = "you";
// COMPUTE MAX OF TWO VALUES OF TYPE string

fn_max(s, t);

int* p1 = &a;
int* p2 = &b;
// COMPUTE MAX FOR TWO POINTERS

//fn_max(p1, p2);

char const* s1 = "George";
char const* s2 = "Bush";
// COMPUTE MAX FOR TWO C-STRINGS

//fn_max(s1, s2);

return 0;
}

[edited by - kelaklub on March 5, 2004 2:13:06 PM] [edited by - kelaklub on March 5, 2004 2:13:48 PM]

##### Share on other sites
Umm... I guess template specialization just can''t do that... all the examples I''ve seen have removed something from within the brackets... so I don''t think you can effectively specialize to "any type which happens to be a pointer" - you just get two template definitions which *both* pick up pointer types (the first one can still apply to int * since int * is a type). Then when you write the char const* specialization, that would be valid, except that it validly specializes both of the previous templates, so the compiler doesn''t know which you want.

Hmm... sucks. There must be a way around this? Seems kind of hard for the C++ enthusiasts to be proud of this language feature if there isn''t a fairly nice workaround...

##### Share on other sites
I read about something called template argument deduction where I guess the compiler is able to figure out which template to use. I don't if maybe a specific type of cast needs to be applied here on the arguments being passed. This is where I was also getting almost similar errors until I applied a static_cast

#include <iostream>using namespace std;// ***************************************************// COMPUTES THE MAXIMUM OF TWO INTEGER VALUES// ***************************************************inline int const& fn_max(int const& x, int const& y){    // IF x < y THEN USE y ELSE USE x    return x < y ? y : x;}// ***************************************************// COMPUTES THE MAXIMUM OF TWO VALUES OF ANY TYPE// ***************************************************template <typename tType>tType const& fn_max(tType const& x, tType const& y){	// IF x < y THEN USE y ELSE USE x    return  x < y ? y : x;}// ***************************************************// COMPUTES THE MAXIMUM OF THREE VALUES OF ANY TYPE// ***************************************************template <typename tType>tType const& fn_max(tType const& x, tType const& y, tType const& z){	return fn_max(fn_max(x, y), z);}int main(){    cout << endl << fn_max(7, 42, 68)                 << endl;  // calls the template for three arguments    cout << endl << fn_max(7.0, 42.0)                 << endl;  // calls fn_max<double> (by argument deduction)    cout << endl << fn_max('a', 'z')                  << endl;  // calls fn_max<char> (by argument deduction)    cout << endl << fn_max(1, 2)                      << endl;  // calls the nontemplate for two ints    cout << endl << fn_max<>(3, 4)                    << endl;  // calls fn_max<int> (by argument deduction)    cout << endl << fn_max<double>(7, 42)             << endl;  // calls max<double> (no argument deduction)    cout << endl << fn_max(static_cast<int>('a'), 47) << endl;  // calls the nontemplate for two ints    	return 0;}

[edited by - kelaklub on March 5, 2004 2:53:55 PM]

[edited by - kelaklub on March 5, 2004 2:55:28 PM]

##### Share on other sites
You first sample code compiles on GCC 3.3.3, the problem is with VC6, which is well known to be very badly behaving when it comes to non trivial template usage.

##### Share on other sites
Haha, good to know man, good to know. Take care.

##### Share on other sites
By the way, Watcom offers a free C++ compiler. Does anyone out there use it and know if it can handle this kind of stuff?

##### Share on other sites
quote:
Original post by kelaklub
Can anyone figure this out? The two function calls at the bottom have been commented out, but when I remove the comments and try compiling this inside VC6, I get the following errors...

error C2667: 'fn_max' : none of 2 overload have a best conversion
error C2668: 'fn_max' : ambiguous call to overloaded function
error C2667: 'fn_max' : none of 2 overload have a best conversion
error C2668: 'fn_max' : ambiguous call to overloaded function

Any suggestions would be most helpful. Thank you.
#include <iostream>#include <cstring>#include <string>using namespace std;// MAXIMUM OF TWO VALUES OF ANY TYPEtemplate <typename tType>inline tType const& fn_max(tType const& a, tType const& b){    return  a < b  ?  b : a;}// MAXIMUM OF TWO POINTERStemplate <typename tType>inline tType* const& fn_max(tType* const& a, tType* const& b){    return  *a < *b  ?  b : a;}// MAXIMUM OF TWO C-STRINGSinline char const* const& fn_max(char const* const& a, char const* const& b){    return strcmp(a, b) < 0  ?  b : a;}int main(){    int a = 7, b = 42;    // COMPUTE MAX OF TWO VALUES OF TYPE int    fn_max(a, b);    string s = "hey";    string t = "you";    // COMPUTE MAX OF TWO VALUES OF TYPE string    fn_max(s, t);    int* p1 = &a;    int* p2 = &b;    // COMPUTE MAX FOR TWO POINTERS    //fn_max(p1, p2);    char const* s1 = "George";    char const* s2 = "Bush";    // COMPUTE MAX FOR TWO C-STRINGS    //fn_max(s1, s2);        return 0;}

Visual C++ is actually not too bad at template stuff. However, the specialization technique you used is inappropriate. This one works for me:

// MAXIMUM OF TWO VALUES OF ANY TYPEtemplate <typename tType>inline tType const& fn_max(tType const& a, tType const& b){    return  a < b  ?  b : a;}// MAXIMUM OF TWO POINTERStemplate <typename tType>inline tType* const& fn_max(tType* const& a, tType* const& b){    return  *a < *b  ?  b : a;}// MAXIMUM OF TWO C-STRINGStemplate < > inline char const* const& fn_max < CString > (char const* const& a, char const* const& b){    return strcmp(a, b) < 0  ?  b : a;}

However I must admit that for slightly more complex operations, VC has made me throw up..

[edited by - psamty10 on March 6, 2004 12:00:53 PM]

[edited by - psamty10 on March 6, 2004 12:01:40 PM]

[edited by - psamty10 on March 6, 2004 12:02:15 PM]

##### Share on other sites
Damn this HTML formatting....