5
#include <iostream>
using namespace std;
struct A {
    A() { cout << "default" << endl; }
    A(const A&) { cout << "copy" << endl; }
    A(A&&) { cout << "move" << endl; }

};
int main() {
    A a{};
    auto aa = A{};
    return 0;
}

This program will print default, default in MSVC2013. Does the standard say anything of creating an object with auto on the left side, or can the second version, auto aa = A{}; first call the default constructor and then move/copy the tmp variable to the left side?

Praetorian
  • 106,671
  • 19
  • 240
  • 328
hidayat
  • 9,493
  • 13
  • 51
  • 66
  • 3
    Look [here](http://coliru.stacked-crooked.com/a/031371d6a96bbb4d). Notice that I used the `-fno-elide-constructors` flag. What you're seeing is called copy elision, search for that and you'll find several answers explaining what it is. And `auto` is completely orthogonal to the issue, you'll see the same behavior even if you use `A` instead. – Praetorian Nov 25 '14 at 17:38
  • is the copy elision optional for the compiler to do? or is it in the standard that it has to? – hidayat Nov 25 '14 at 17:43
  • I do not know of any case where copy elision is mandatory. – ThomasMcLeod Nov 25 '14 at 17:50
  • 1
    This is not exactly a dupp. – ThomasMcLeod Nov 25 '14 at 17:54

0 Answers0