考虑这段代码:
#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
}
实际上似乎可以编译的唯一版本是ret3()
. 实际上,如果我省略了实现并仅声明它,它就会编译(当然永远不会链接),但实际上我不知道如何显式返回对数组的右值引用。如果这不能发生,那么我可以得出结论,对数组的右值引用不是被禁止的,而是不能使用的?
编辑:
我刚刚意识到这有效:
array&& ret3(){
return std::move(sample);
}
现在有趣的是了解它的实际价值......