Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invalid initialization of non-const reference of type

In the following code, I'm not able to pass a temporary object as argument to the printAge function:

struct Person {
  int age;
  Person(int _age): age(_age) {}
};

void printAge(Person &person) {
   cout << "Age: " << person.age << endl;
}

int main () {
  Person p(50);
  printAge(Person(50));  // fails!
  printAge(p);
  return 0;
}

The error I get is:

error: invalid initialization of non-const reference of type ‘Person&’ from an rvalue of type ‘Person’

I realize that this is something to do with passing an lValue to a function expecting a rValue... Is there a way to convert my lValue to rValue by using std::move or something? I tried taking a constant parameter, but that does not seem to work.

like image 288
jeffreyveon Avatar asked Nov 27 '14 13:11

jeffreyveon


2 Answers

Simply make your print function take your argument by const&. This is also logically right as it doesn't modify your argument.

void printAge(const Person &person) {
   cout << "Age: " << person.age << endl;
}

The actual problem is the other way around. You are passing a temporary(rvalue) to a function which expects an lvalue.

like image 158
Stephan Dollberg Avatar answered Oct 31 '22 18:10

Stephan Dollberg


Or, if you have a C++11-compliant compiler, can use the so called universal reference approach, which, via reference collapsing rules, can bind to both lvalue and rvalue references:

#include <iostream>
using namespace std;

struct Person {
  int age;
  Person(int _age): age(_age) {}
};

template<typename T> // can bind to both lvalue AND rvalue references
void printAge(T&& person) {
   cout << "Age: " << person.age << endl;
}

int main () {
  Person p(50);
  printAge(Person(50));  // works now
  printAge(p);
  return 0;
}

Or, in C++14,

void printAge(auto&& person) {
   cout << "Age: " << person.age << endl;
}
like image 40
vsoftco Avatar answered Oct 31 '22 16:10

vsoftco