what about std::make_pair?

This is a discussion on what about std::make_pair? within the C++ Programming forums, part of the General Programming Boards category; hi, what is it for? stl docu says template <class T1, class T2> pair<T1, T2> make_pair(const T1& x, const T2& ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    519

    what about std::make_pair?

    hi,

    what is it for?

    stl docu says

    template <class T1, class T2>
    pair<T1, T2> make_pair(const T1& x, const T2& y)

    Equivalent to pair<T1, T2>(x, y). This is a global function, not a member function. It exists only for the sake of convenience.
    looking at the implementation of the VS2005 stl is seems to do something else:

    Code:
    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));
    	}
    it takes the pair members by value instead of const reference adding 2 copy constructor calls to one constructor call to pair(), a copy constructor call for the the return by value and another copy constructor call if the client code also catches by value instead of by const reference ... compared to a singe constructor call to pair().

    what kind of convenience is this? has std::make_pair any sense/advantage in any situation?

    thank you!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,921
    Quote Originally Posted by pheres
    it takes the pair members by value instead of const reference adding 2 copy constructor calls to one constructor call to pair(), a copy constructor call for the the return by value and another copy constructor call if the client code also catches by value instead of by const reference ... compared to a singe constructor call to pair().
    Return value optimisation may make all these extra copies amount to nothing.

    Quote Originally Posted by pheres
    what kind of convenience is this? has std::make_pair any sense/advantage in any situation?
    The primary advantage is that it allows you to avoid having to repeat yourself by explicitly stating the template arguments.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    That sounds reasonable. thanks!

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The standard library has a lot of "convenience" functions like that (in functional). I often add similar functions to classes where most/all arguments could be deduced from the constructor - particularly if the arguments have complex types.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21