C++ g+中的右值引用+;

C++ g+中的右值引用+;,c++,C++,我正在将编译器从VisualStudio更改为g++,并且在函数参数中遇到了一些通过引用传递的问题 在Visual Studio中,功能是: void Foo(int&a, int&b) 所以a,b在这个函数中被修改。因此我不能在g中使用++ void Foo(const int&a, const int &b) 我的g++中也不允许使用右值引用: void Foo( int&& a, int&& b) 那么,使用指针是转换代

我正在将编译器从VisualStudio更改为g++,并且在函数参数中遇到了一些通过引用传递的问题

在Visual Studio中,功能是:

void Foo(int&a, int&b)
所以a,b在这个函数中被修改。因此我不能在g中使用++

void Foo(const int&a, const int &b)
我的g++中也不允许使用右值引用:

void Foo( int&& a, int&& b)
那么,使用指针是转换代码的唯一方法吗

void Foo( int* a, int* b)
p/S:这是使用g++编译时的错误:

error: no matching function for call to ‘Steerable::buildSCFpyrLevs(Tensor<double, 2ul>, std::vector<Tensor<double, 2ul> >&, int&, int&, int&, bool&)’
Steerable.cpp:63:100: note: candidate is:
Steerable.h:93:7: note: void Steerable::buildSCFpyrLevs(Steerable::data_ref, std::vector<Tensor<double, 2ul> >&, int, int, int, bool)
Steerable.h:93:7: note:   no known conversion for argument 1 from ‘Tensor<double, 2ul>’ to ‘Steerable::data_ref {aka Tensor<double, 2ul>&}’

FreqComplexFilter
可能不会通过引用返回

肮脏的修补程序:

Tensor<double, 2> tempVal = imdft.FreqComplexFilter(toComplex(lo0mask));
buildSCFpyrLevs(tempVal, pyr_freq, nLevel, nDir, twidth, subsample);
张量tempVal=imdft.FreqComplexFilter(toComplex(lo0mask)); 构建SCFPYRLEVS(临时、pyr_freq、nLevel、nDir、twidth、子样本);
它是脏的,因为它只是让代码编译,它没有解决底层的设计问题(问题是为什么
buildSCFpyrLevs
正在修改
FreqComplexFilter
返回的临时值)。

为什么第一个不起作用?呃,你想做什么?转换代码做什么?至于第二个,我想你可以使用
const int&
。否则,您还必须更改调用代码“因此我不能在g++中使用”:错误。你可以用它。只是不使用临时变量。我将使用@juanchopanza,并说您的代码将临时变量作为非常量引用传递。你说必须修改参数,那你为什么要传递一个临时参数?如果你想修改一些东西,让它成为你以后可以实际使用的东西。谢谢你,它很有用!你能进一步解释一下,为什么我把整个函数放在buildSCFpyrLevs中时,它不起作用?@Dzunguyen我不完全确定编译器的内部工作方式,但我知道
FreqComplexFilter
隐式返回一个临时值(临时,因为它的作用域仅限于表达式)在我看来,应该尽可能避免通过引用传递临时值,因为和g++强制这种行为是好的。
buildSCFpyrLevs(imdft.FreqComplexFilter(toComplex(lo0mask)),pyr_freq,nLevel,nDir,twidth, subsample);
Tensor<double, 2> tempVal = imdft.FreqComplexFilter(toComplex(lo0mask));
buildSCFpyrLevs(tempVal, pyr_freq, nLevel, nDir, twidth, subsample);