Rvalue-reference for an array: can this really happen?

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 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 ...

+5
source share
1 answer

, r. . , . , print_sorted(array&), . - , . print_sorted(array&&) inplace, , .

r- .

+3

All Articles