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

后端 未结 7 1310
借酒劲吻你
借酒劲吻你 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:18

    make_pair creates an extra copy over the direct constructor. I always typedef my pairs to provide simple syntax.
    This shows the difference (example by Rampal Chaudhary):

    class Sample
    {
        static int _noOfObjects;
    
        int _objectNo;
    public:
        Sample() :
            _objectNo( _noOfObjects++ )
        {
            std::cout<<"Inside default constructor of object "<<_objectNo< map;
    
        map.insert( std::make_pair( 1, sample) );
        //map.insert( std::pair( 1, sample) );
        return 0;
    }
    

提交回复
热议问题