为什么std :: make_pair返回一对引用类型

时间:2014-04-29 15:13:18

标签: c++ std-pair

我从Koffman和Wolfgang那里学习了教科书中的配对。他们给make_pair的示例实现如下所示:

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

我不明白为什么他们使用Type1&amp;和Type2&amp;用于在return语句中实例化对模板,而不仅仅是Type1和Type2。

1 个答案:

答案 0 :(得分:6)

据我所知make_pair有以下原型:

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

(注意:自C ++ 11以来略有不同)

另见http://en.cppreference.com/w/cpp/utility/pair/make_pair

这也是标准在§20.3.3 [pairs.spec]中所说的内容。所以这一定是书中的错误(或者你可能忽略了一些东西)。

相关问题