参照以下代码:C++函数和右值混乱
// Example program
#include <iostream>
#include <string>
using namespace std;
struct S
{
S()
{
cout << "ctor\n";
}
S(S&& rhs)
{
cout << "called move\n";
}
};
S goo()
{
S a;
return a;
}
int main()
{
S&& goo();
cout << "before construction\n";
S a = goo();
}
//http://thbecker.net/articles/rvalue_references/section_05.html
为什么是代码调用move constructor
而不是功能S goo()
?如果你注释掉第一行,那么它不会。
为什么S goo()
的返回类型根据main
的第一行而不同?我不认为这甚至应该编译,但这里编译 http://cpp.sh/22zeq
(上wandbox不会编译:https://wandbox.org/permlink/3YxBdcWs91FRiODG)
在阅读这里的例子:http://thbecker.net/articles/rvalue_references/section_05.html 当我偶然发现了这个
你觉得呢'S &&咕();在主'呢? – SergeyA
@SergeyA这里没有'S && goo()'的定义,所以它是如何工作的? – PYA