Why does std :: make_pair get a value by value instead of a reference to const?

Referring to this website http://www.cplusplus.com/reference/std/utility/make_pair/

Std :: make_pair has this signature (and a possible implementation):

template <class T1,class T2> pair<T1,T2> make_pair (T1 x, T2 y) { return ( pair<T1,T2>(x,y) ); } 

I am wondering why std :: make_pair has an input parameter by value rather than a const reference?

Is there any specific reason for this?

+6
source share
2 answers

Initially, it took parameters by const link, but this led to some unexpected problems. It has been changed to jump by value after defect report:

http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#181

It is expected that the compiler will inline this function and will be able to optimize the missing parameter most of the time.

+6
source

So std::make_pair( "abc", 3 ) will work. If std::make_pair took the link, the type deduced for T1 would be char const[4] , which would create all kinds of strange error messages because it is not copied.

+3
source

Source: https://habr.com/ru/post/910901/


All Articles