c++ - How is a method taking const char* as argument a near match to a method taking const int&? -
the following code throws compiler error when compile it.
template <typename t> inline t const& max (t const& a, t const& b) { return < b ? b : a; } // maximum of 2 c-strings (call-by-value) inline char const* max (char const* a, char const* b) { return strcmp(a,b) < 0 ? b : a; } // maximum of 3 values of type (call-by-reference) template <typename t> inline t const& max (t const& a, t const& b, t const& c) { return max (max(a,b), c); } int main () { ::max(7, 42, 68); }
on compilation error :
error: call of overloaded 'max(const int&, const int&)' ambiguous
note: candidates are:
note: const t& max(const t&, const t&) [with t =int]
note: const char* max(const char*, const char*)
how max(const char*, const char*) becomes near match max(const int&, const int &) when have template method matches call?
i bet have using namespace std
in code. remove , you'll fine.
compare: http://ideone.com/csq8sv , http://ideone.com/iqaoi6
if strictly need namespace std used, can force root namespace call (the way in main()
):
template <typename t> inline t const& max (t const& a, t const& b, t const& c) { return ::max(::max(a,b), c); }
Comments
Post a Comment