Comeau is OK with the OP's code if you move the int definition.
It's also happy if you move the int definition and change it to a specialised function template.Code:#include <iostream> using namespace std; // maximum of two int values inline int const& max (int const& a, int const& b) { cout << "inline int const& max (int const& a, int const& b)" << endl; return a < b ? b : a; } // maximum of two values of any type template <typename T> inline T const& max (T const& a, T const& b) { cout << "inline T const& max (T const& a, T const& b)" << endl; return a < b ? b : a; } // maximum of three values of any type template <typename T> inline T const& max (T const& a, T const& b, T const& c) { cout << "inline T const& max (T const& a, T const& b, T const& c)" << endl; return max (max(a,b), c); // uses the template version even for ints } // because the following declaration comes // too late: int main() { int a, b, c; a = 1; b = 2; c = 3; max(a,b,c); getchar(); return 0; }
Though you must remove the using namespace std; otherwise the STL max definitions conflict.Code:// maximum of two int values template <> inline int const& max<int>(int const& a, int const& b) { std::cout << "inline int const& max (int const& a, int const& b)" << std::endl; return a < b ? b : a; }




Reply With Quote