LearnFrenzy
Sign in Take a free test

Move Semantics · Predict the Output · Question 1

Constructing from a named object and a temporary Answer, explanation & discussion

  • Output
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 1 Back to Exercise

What is the output of the following C++ program on a 64-bit system?

main.cpp
#include <iostream>
#include <utility>

struct W {
    W() = default;
    W(const W&) { std::cout << "C"; }
    W(W&&) noexcept { std::cout << "M"; }
};

W make() { return W(); }

int main() {
    W a;
    W b(a);
    W c(std::move(a));
    W d(make());
    W e(std::move(b));
    std::cout << '\n';
    return 0;
}
  • A CMM Correct
  • B CMMM
  • C CCMM
  • D CMCM
Correct Answer: Option A

b copies a, c and e are moves. d is built directly from the prvalue returned by make() – guaranteed elision, so nothing is printed.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links