1

The following invokes one instance of the constructor followed by the destructor of A (vs. invoking copy or move constructors). Is this an optimization that only one instance of A is created, or is this behavior mandated by the standard?

#include <iostream>
struct A {
    A() { 
        std::cout << "Constructor\n";
    }
    ~A() { 
        std::cout << "Destructor\n";
    }
    A (const A&) = delete;
};

int main() { 
    A a{A{A{A{A{}}}}};
}
user3882729
  • 1,339
  • 8
  • 11
  • From C++17, it is **mandated**. Read about copy elision. Look at `T x = T(T(f()));` example given [here](https://en.cppreference.com/w/cpp/language/copy_elision) – Jason Jan 30 '23 at 08:50

0 Answers0