What is the purpose of std::make_pair vs the constructor of std::pair?

后端 未结 7 1312
借酒劲吻你
借酒劲吻你 2020-12-04 06:04

What is the purpose of std::make_pair?

Why not just do std::pair(0, \'a\')?

Is there any difference between the tw

7条回答
  •  不知归路
    2020-12-04 06:39

    It's worth noting that this is a common idiom in C++ template programming. It's known as the Object Generator idiom, you can find more information and a nice example here.

    Edit As someone suggested in the comments (since removed) the following is a slightly modified extract from the link in case it breaks.

    An Object Generator allows creation of objects without explicitly specifying their types. It is based on a useful property of function templates which class templates don't have: The type parameters of a function template are deduced automatically from its actual parameters. std::make_pair is a simple example that returns an instance of the std::pair template depending on the actual parameters of the std::make_pair function.

    template 
    std::pair  
    make_pair(T t, U u)
    {
      return std::pair  (t,u);
    }
    

提交回复
热议问题