为什么make_pair <int,int>在C ++ 11中失败?

giu*_*sti 3 c++ stl c++11

下面的代码在以C ++ 98编译时可以正常工作,但在C ++ 11上失败。为什么?

#include <iostream>
#include <utility>

using namespace std;

int main()
{
    int u = 1;
    pair<int, int> p = make_pair<int, int>(0, u);
    cout << p.first << " " << p.second << "\n";
}
Run Code Online (Sandbox Code Playgroud)

来自g ++(Debian 8.3.0-6)8.3.0的错误消息是:

foo.cpp: In function ‘int main()’:
foo.cpp:9:45: error: no matching function for call to ‘make_pair<int, int>(int, int&)’
  pair<int, int> p = make_pair<int, int>(0, u);
                                             ^
Run Code Online (Sandbox Code Playgroud)

我知道我可以简单地通过从中删除模板说明符make_pair并让编译器自行决定类型来进行编译。但是我有兴趣了解从C ++ 98到C ++ 11的哪些变化,从而使该代码不再兼容。

raf*_*x07 7

在c ++ 11之前,

template< class T1, class T2 >
std::pair<T1,T2> make_pair( T1 t, T2 u );
Run Code Online (Sandbox Code Playgroud)

您将T1和T2定义为:int,因此int可以采用Lvalues和Rvalues。

从c ++ 11开始

template< class T1, class T2 >
std::pair<V1,V2> make_pair( T1&& t, T2&& u );
Run Code Online (Sandbox Code Playgroud)

因为T2被定义为int,所以int&&第二个参数只能采用Rvalues。但是u是左值。