I tried to write some algorithm using recursive function and met this:
#include<vector>
using namespace std;
void f(vector<int>&& v) {
if (!v.empty()) {
// do some work
v.pop_back();
f(v); // compilation error!!!
}
return;
}
int main() {
f(vector<int>{1, 2, 3});//ok
return 0;
}
It doesn't compile, error message:
x86-64 clang 14.0.0
-std=c++11
<source>:7:9: error: no matching function for call to 'f'
f(v);
^
<source>:3:6: note: candidate function not viable: expects an rvalue for 1st argument
void f(vector<int>&& v) {
^
1 error generated.
The reason I used "&&" is that I hope it could receive some r-value input parameter and use it as reference(not copy). I also tried to change f
into generic function:
template<typename T>
void f(vector<T>&& v) {
Still fails.
How to fix this?