mercredi 26 juin 2019

Why does make_pair

The following piece of code works fine when compiling as C++ 98, but it fails as C++ 11. Why?

#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";
}

The error message from g++ (Debian 8.3.0-6) 8.3.0 is:

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);
                                             ^

I'm aware that I can compile this simply by removing the template specifier from make_pair and letting the compiler decide the types on its own. But I'm interested in understanding what changes from C++ 98 to C++ 11 that makes this code no longer compliant.

Aucun commentaire:

Enregistrer un commentaire