dimanche 2 avril 2017

What does mean an `int&&` as function argument in C++? [duplicate]

This question already has an answer here:

What does mean an int&& as function arguments in C++?

Case 1:

#include <iostream>
using namespace std;
 //Compiler version g++ 4.9

 int f(int&& i)
 {
    cout<<i<<endl;
 }

 int main()
 {
  f(1);
 }

It's working fine.

Case 2:

#include <iostream>
using namespace std;
 //Compiler version g++ 4.9

 int f(int&& i)
 {
    cout<<i<<endl;
 }

 int main()
 {
    int i = 1;
    f(i);
 }

Compiler gives an error.

In function 'int main()':
error: cannot bind 'int' lvalue to 'int&&'
f(i);

Why second case give me an error?

Aucun commentaire:

Enregistrer un commentaire