Question

I'm learning about pairs from the textbook by Koffman and Wolfgang. The sample implementation they gave of make_pair looks like this:

template<typename Type1, typename Type2>
  make_pair(const Type1& first_value, const Type2& second_value) {
    return pair<Type1&, Type2&>(first_value, second_value);
  }

I do not understand why they use Type1& and Type2& for instantiating the pair template in the return statement instead of just Type1 and Type2.

Was it helpful?

Solution

As far as I know make_pair has the following prototype:

template<class Type1, class Type2>
std::pair<Type1,Type2> make_pair(Type1 first_value, Type2 second_value);

(Note: slightly different since C++11)

See also http://en.cppreference.com/w/cpp/utility/pair/make_pair.

This is also what the standard says in §20.3.3 [pairs.spec]. So this must be an error in the book (or you may have overlooked something).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top