C++ 类型的非常量引用的初始化无效

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27172033/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 20:44:45  来源:igfitidea点击:

Invalid initialization of non-const reference of type

c++pass-by-referencelvalue-to-rvalue

提问by jeffreyveon

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

在以下代码中,我无法将临时对象作为参数传递给printAge函数:

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.

我意识到这与将 lValue 传递给期望 rValue 的函数有关...有没有办法使用 std::move 或其他方法将我的 lValue 转换为 rValue?我尝试采用常量参数,但这似乎不起作用。

回答by Stephan Dollberg

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

只需让您的打印函数接受您的参数const&。这在逻辑上也是正确的,因为它不会修改您的论点。

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.

实际的问题是相反的。您正在将临时(右值)传递给需要左值的函数。

回答by vsoftco

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:

或者,如果您有一个 C++11 兼容的编译器,可以使用所谓的通用引用方法,通过引用折叠规则,它可以绑定到左值和右值引用:

#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,

或者,在 C++14 中,

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

回答by telcom

Your code doesn't work if you run g++ or gcc compilers. You need to add constto void printAge(const Person &person). However, in Visual Studio it will work fine. I've tested for VS2010 and VS2012 and in both the following code works fine.

如果您运行 g++ 或 gcc 编译器,您的代码将不起作用。您需要添加constvoid printAge(const Person &person). 但是,在 Visual Studio 中它可以正常工作。我已经对 VS2010 和 VS2012 进行了测试,并且在以下代码中都可以正常工作。

 #include<iostream>

using namespace std;
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));  // DOES NOT fail!
  printAge(p);
  return 0;
}