with reference to the following code:
// 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
why is the code calling the move constructor
and not the function S goo()
? If you comment out the first line then it does not.
why is the return type of S goo()
different depending on the first line in main
? I dont think this should even compile but compiles here
http://cpp.sh/22zeq
(does not compile on wandbox: https://wandbox.org/permlink/3YxBdcWs91FRiODG)
was reading an example on here: http://thbecker.net/articles/rvalue_references/section_05.html when i stumbled upon this
It is because
S&& goo();
is declaration of new function. But when you compile it exists only one function with such nameS goo();
that is why it was called.It is a work of linker. It has not found any another function with such name and linked it to
S goo();