I have the following simple function template:
max.hpp:
template <typename T>
inline T const& max (T const& a, T const& b)
{
return a <b? b :a;
}
max.cpp
#include <iostream>
#include <string>
#include "max.hpp"
int main()
{
int i = 42;
std::cout << "max(7,i): " << ::max(7,i) << std::endl;
double f1 = 3.4;
double f2 = -6.7;
std::cout << "max(f1,f2): "<< ::max(f1,f2) << std::endl;
std::string s1 = "mathematics";
std::string s2 = "math";
std::cout << "max(s1,s2): " << ::max(s1,s2) << std::endl;
std::cin.get();
return 0;
}
This works fine, since ::
will tell compiler to search for max
template in global namespace. However, when I remove those ::
in those 3 cases, I got the following ambiguity error message:
max.cpp: In function ‘int main()’:
max.cpp:21:43: error: call of overloaded ‘max(std::string&, std::string&)’ is ambiguous
max.hpp:2:17: note: candidates are: const T& max(const T&, const T&)
[with T = std::basic_string<char>]
/usr/lib/gcc/i686-pc-cygwin/4.5.3/include/c++/bits/stl_algobase.h:209:5:
note: const _Tp& std::max(const _Tp&, const _Tp&)
[with _Tp = std::basic_string<char>]
My questions are as follows:
In this case, does the compiler search for
std
namespace by default even I did not include the header file<algorithm>
wherestd::max
is located?What are placed in the
global namespace
? Is it required to use the::
when calling functions inglobal namespace
under all circumstances?Why there is no ambiguity when
max
is applied oninteger
andfloat
type, only has ambiguity forstring
type?
FYI: I am using gcc 4.5.3 for compiling. Thank you!