It relies on the fact that the std :: string constructor accepts const char *. It doesn't matter if this constructor is std :: string explicit or not. The template subtracts the type and uses the pair copy constructor to convert it. It also doesn't matter if the constructor is an explicit pair.
If you turn the std :: string constructor into:
class string
{
public:
string(char* s)
{
}
};
you will get this error:
/usr/include/c++/4.3/bits/stl_pair.h: In constructor βstd::pair<_T1, _T2>::pair(const std::pair<_U1, _U2>&) [with _U1 = const char*, _U2 = int, _T1 = const string, _T2 = int]β:
foo.cpp:27: instantiated from here
/usr/include/c++/4.3/bits/stl_pair.h:106: error: invalid conversion from βconst char* constβ to βchar*β
/usr/include/c++/4.3/bits/stl_pair.h:106: error: initializing argument 1 of βstring::string(char*)β
The constructor looks like this:
template<class _U1, class _U2>
pair(const pair<_U1, _U2>& __p)
: first(__p.first),
second(__p.second) { }
The copy constructor is as follows:
template<class _U1, class _U2>
pair(const pair<_U1, _U2>& __p)
: first(__p.first),
second(__p.second) { }
source
share