Consider this code:
#include <iostream>
using namespace std;
typedef int array[12];
array sample;
array ret1(){
return sample;
}
array& ret2(){
return sample;
}
array&& ret3(){
return sample;
}
void eat(array&& v){
cout<<"got it!"<<endl;
}
int main(){
eat(ret1());
eat(ret2());
eat(ret3());
}
The only version that actually compiles is this ret3(). In fact, if I do not use the implementation and just declare it, it compiles (and never references, of course), but in fact I do not know how to explicitly return the rvalue reference to an array. If this cannot be, then can I conclude that rvalue-reference for arrays is not prohibited, but simply can not be used?
EDIT:
I just realized that this works:
array&& ret3(){
return std::move(sample);
}
now pleasure realizes that it really is worth ...
source
share