Templates and Different Compilers

D

demibee

I just downloaded DJGPP 2.03 and I wanted to test it on something
simple. Unfortunately, I chose something I know little about -- C++
templates. (The code is practically copied directly from MS's own
webpages.) The following compiles in VS6 with 0 errors and warnings,
but the DJGPP compile gives errors indicating that the call to "min"
in the main program is ambiguous (errors are shown below the code).

Have I implemented the template incorrectly? Have I used gxx
incorrectly? Is VS lax when it comes to picking out problems? (I
notice it doesn't warn me that argc and argv are never used.)

Anyway, the code and DJGPP's error messages...

----------

/* TEST.CPP -
*/



#include<iostream>
#include<string>



using namespace std;



template <class T>
T min (T a, T b)
{
return (a < b) ? a : b;
}



int main (int argc, char *argv[])
{

cout << "min(5, 10) is: " << min(5, 10) << endl;
cout << "min(2.5, 1.0) is: " << min(2.5, 1.0) << endl;
cout << "min('A', 'a') is: " << min('A', 'a') << endl;

return 0;

} // end main

----------
C:\SRC\CPP> gxx test.cpp -o test.exe
test.cpp: In function `int main(int, char**)':
test.cpp:27: error: call of overloaded `min(int, int)' is ambiguous
test.cpp:18: error: candidates are: T min(T, T) [with T = int]
C:/APPS/DOS/DJGPP/lang/cxx/3.32/bits/stlalgobase.h:149: error:
const _Tp& std::min(const _Tp&, const _Tp&) [with _Tp = int]
test.cpp:28: error: call of overloaded `min(double, double)' is
ambiguous
test.cpp:18: error: candidates are: T min(T, T) [with T = double]
C:/APPS/DOS/DJGPP/lang/cxx/3.32/bits/stlalgobase.h:149: error:
const _Tp& std::min(const _Tp&, const _Tp&) [with _Tp = double]
test.cpp:29: error: call of overloaded `min(char, char)' is ambiguous
test.cpp:18: error: candidates are: T min(T, T) [with T = char]
C:/APPS/DOS/DJGPP/lang/cxx/3.32/bits/stlalgobase.h:149: error:
const _Tp& std::min(const _Tp&, const _Tp&) [with _Tp = char]
 
C

CrayzeeWulf

demibee said:
Have I implemented the template incorrectly? Have I used gxx
incorrectly? Is VS lax when it comes to picking out problems? (I
notice it doesn't warn me that argc and argv are never used.)
A template with the same name and number of parameters is part of the "std"
namespace in the header <algorithm>. DJGPP probably includes this through
<string> and results in the conflict. You can just use the standard min()
template by explicitly including <algorithm> as follows:

// -------------------------------------------------------------------
#include <iostream>
#include <string>
#include <algorithm>


int main (int argc, char *argv[])
{
using namespace std ;
cout << "min(5, 10) is: " << min(5, 10) << endl ;
cout << "min(2.5, 1.0) is: " << min(2.5, 1.0) << endl ;
cout << "min('A', 'a') is: " << min('A', 'a') << endl ;
return 0;
} //end main
// -------------------------------------------------------------------

Otherwise, if you insist on using your own template, consider the following
workaround by not injecting everything from the "std" namespace into the
global namespace (i.e. remove "using namespace std"):

#include <iostream>
#include <string>

template <class T>
T min (T a, T b)
{
return (a < b) ? a : b;
}

int main (int argc, char *argv[])
{
std::cout << "min(5, 10) is: " << min(5, 10) << std::endl ;
std::cout << "min(2.5, 1.0) is: " << min(2.5, 1.0) << std::endl ;
std::cout << "min('A', 'a') is: " << min('A', 'a') << std::endl ;
return 0;
} //end main


Hope that helps,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top