c++中的右值引用

Rvalue references in g++

本文关键字:引用 c++      更新时间:2023-10-16

我正在将编译器从Visual Studio更改为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>&}’

函数声明如下:

typedef Tensor<value_type,2> data_type;
typedef data_type& data_ref;

vector<Steerable::data_type>& Steerable::buildSCFpyr(Steerable::c_data_ref im, int nLevel, int nDir, int twidth, bool subsample)

出现错误的行:

buildSCFpyrLevs(imdft.FreqComplexFilter(toComplex(lo0mask)),pyr_freq,nLevel,nDir,twidth, subsample);

FreqComplexFilter可能没有通过引用返回。

Tensor<double, 2> tempVal = imdft.FreqComplexFilter(toComplex(lo0mask));
buildSCFpyrLevs(tempVal, pyr_freq, nLevel, nDir, twidth, subsample);

这是肮脏的,因为它只是使代码编译,它不解决潜在的设计问题(问题是为什么buildSCFpyrLevs正在修改FreqComplexFilter返回的临时值)。