これは、std::stringのコンストラクターがconstchar*を受け入れるという事実に依存しています。std::stringのこのコンストラクターが明示的であるかどうかは関係ありません。テンプレートは型を差し引き、ペアのコピーコンストラクターを使用して変換します。ペアコンストラクタが明示的であるかどうかも関係ありません。
std :: stringのコンストラクターを次のように変換すると、次のようになります。
class string
{
public:
string(char* s)
{
}
};
このエラーが発生します:
/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*)’
コンストラクターは次のようになります。
template<class _U1, class _U2>
pair(const pair<_U1, _U2>& __p)
: first(__p.first),
second(__p.second) { }
コピーコンストラクタは次のようになります。
template<class _U1, class _U2>
pair(const pair<_U1, _U2>& __p)
: first(__p.first),
second(__p.second) { }