Why does the call to get_data2 () lead to an additional call to c-tor (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(); }
data get_data2() { return std::move(create_data()); }
data get_data1() { return create_data(); }
int main() { data var = get_data1(); return 0; }
Exit with get_data2 ():
cted(): 0x7ffffb2cd3df
cted(&&): 0x7ffffb2cd40f
dted(): 0x7ffffb2cd3df
dted(): 0x7ffffb2cd40f
Exit with get_data1 ():
cted(): 0x7ffffd7f230f
dted(): 0x7ffffd7f230f
bobah source
share