In the below example I want to find out why the copy constructor isn't called when I return an automatic variable from the doit() function. I understand that the first version of handler is called because we have a temporary object but can't figure out why the copy constructor isn't called while creating that temporary object (copies everything from s to a temporary object).
#include <iostream>
using namespace std;
class S{
public:
S(){std::cout<<"Constructor\n";}
S(const S& s){std::cout<<"Copy Constructor\n";}
~S(){std::cout<<"Destructor\n";}
};
S doit(){
S s;
return s;
}
void handler(S&& r){
std::cout<<"Here\n";
}
void handler(const S& r){
std::cout<<"Here2\n";
}
int main() {
handler(doit());
}