Consider this code:
#include <iostream>
using namespace std;
typedef int array[12];
array sample;
array ret1(){ //won't compile
return sample;
}
array& ret2(){
return sample;
}
array&& ret3(){
return sample; //won't compile
}
void eat(array&& v){
cout<<"got it!"<<endl;
}
int main(){
eat(ret1());
eat(ret2()); //won't compile
eat(ret3()); //compiles, but I don't really know how to write a function that returns a rvalue-reference to an array
}
The only version that actually seems to compile is ret3()
. In fact, if I leave out the implementation and just declare it, it compiles (and never link of course), but really I don't know how to explicitly return a rvalue reference to an array. If this can't happen, then can I conclude that rvalue-reference to arrays aren't forbidden but just can't be used?
EDIT:
I just realized that this works:
array&& ret3(){
return std::move(sample);
}
now the fun is understanding what it's actually worth...