What is the purpose of std::make_pair?
Why not just do std::pair<int, char>(0, 'a')?
Is there any difference between the two methods?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
(This answer is only correct for C++14 and earlier standards, due to CTAD)
The difference is that with
std::pairyou need to specify the types of both elements, whereasstd::make_pairwill create a pair with the type of the elements that are passed to it, without you needing to tell it. That’s what I could gather from various docs anyways.See this example from http://www.cplusplus.com/reference/std/utility/make_pair/
Aside from the implicit conversion bonus of it, if you didn’t use make_pair you’d have to do
every time you assigned to one, which would be annoying over time…