Why calling get_data2() results in additional c-tor call (g++ 4.7.1 -std=c++11 -O3)?
Code:
#include <iostream>
struct data {
data(data&&){std::cout << "cted(&&): " << (void*) this << std::endl; }
data(data const&){std::cout << "cted(c&): " << (void*) this << std::endl; }
data(){std::cout << "cted(): " << (void*) this << std::endl; }
~data(){std::cout << "dted(): " << (void*) this << std::endl; }
};
data create_data() { return data(); }
// with this one the c-tor is called twice
data get_data2() { return std::move(create_data()); }
// with this one the c-tor is called once
data get_data1() { return create_data(); }
int main() { data var = get_data1(); return 0; }
Output with get_data2():
cted(): 0x7ffffb2cd3df
cted(&&): 0x7ffffb2cd40f
dted(): 0x7ffffb2cd3df
dted(): 0x7ffffb2cd40f
Output with get_data1():
cted(): 0x7ffffd7f230f
dted(): 0x7ffffd7f230f