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 _a
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
using namespace std;
struct Person {
int age;
Person(int _age): age(_age) {}
};
template // 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;
}