Question 14
Back to Exercise
What is the output of the following C++ program on a 64-bit system?
#include <iostream>
#include <memory>
#include <utility>
#include <vector>
struct Job {
std::unique_ptr<int> data;
explicit Job(int v) : data(std::make_unique<int>(v)) {}
};
int main() {
std::vector<Job> jobs;
Job j(5);
jobs.push_back(std::move(j)); // copy would not compile
jobs.emplace_back(8);
auto taken = std::move(jobs.front());
std::cout << jobs.size() << ' ' << (jobs[0].data == nullptr) << ' ' << *taken.data << ' ' << *jobs[1].data << '\n';
return 0;
}
- A 2 1 5 8 Correct
- B 1 0 5 8
- C 2 0 5 8
- D 2 1 8 5
Correct Answer: Option A
Job is move-only because of its unique_ptr member (rule of zero). Moving an element out leaves an empty Job in the vector – the size does not change.
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!