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
}- A. L1 only
- B. L1 and L3
- C. L2 and L3
- 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
- What is printed? cpp int main() { const string s = "x"; string t = std::move(s); cout << s << t; }
- Which line fails to compile? cpp int main() { int x = 1; const int& r = x; auto a = r; // L1 a++; // L2 decltype(r) b = x; // L3 b++; //…
- Which line fails to compile? cpp struct A { virtual ~A() { } }; struct B { virtual ~B() { } }; struct C : A, B { }; int main() { A a = new…
- What does the C++ standard say about this code? cpp const int x = 5; int p = constcast<int>(&x); p = 6;
- For the call f(g(), h()) in C++17, which statement is correct?
- What happens when this is compiled? cpp struct P { int a; private: int b; }; int main() { P p{1, 2}; }