The definition for make_pair in the MSVC++ “utility” header is:
template<class _Ty1,
class _Ty2> inline
pair<_Ty1, _Ty2> make_pair(_Ty1 _Val1, _Ty2 _Val2)
{ // return pair composed from arguments
return (pair<_Ty1, _Ty2>(_Val1, _Val2));
}
I use make_pair all the time though without putting the argument types in angle brackets:
map<string,int> theMap ;
theMap.insert( make_pair( "string", 5 ) ) ;
Shouldn’t I have to tell make_pair that the first argument is std::string and not char* ?
How does it know?
Function template calls can usually avoid explicit template arguments (ie
make_pair<…>) by argument deduction, which is defined by C++03 §14.8.2. Excerpt:The specific rules are a bit complicated, but typically it “just works” as long as you have only one specialization which is generally qualified enough.
Your example uses two steps of deduction and one implicit conversion.
make_pairreturns apair<char const*, int>,template<class U, classV> pair<string,int>::pair( pair<U,V> const & )kicks in withU = char*, V = intand performs member-wise initialization,string::string(char*).