ProgrammingVery hardProduct-company placement · C++

Which line fails to compile (C++17)?

Which line fails to compile (C++17)?
struct A {
    A() = default;
    A(const A&) = delete;
    A(A&&) = default;
};
A make() { A a; return a; }   // L1
int main() {
    A x = make();             // L2
    A y = x;                  // L3
}
  1. A. L1 only
  2. B. L1 and L3
  3. C. L2 and L3
  4. D. L3 only

Correct answer: D. L3 only

Returning a local variable by name first tries to treat it as an rvalue (implicit move), so the move constructor is used in L1. L2 initialises from a prvalue (guaranteed elision). Only copying the lvalue x needs the deleted copy constructor.

More OOP with C++ and Java questions

Exams that ask this topic