Practice mode C++

Assuming standard headers and using namespace std, what does this C++ code print? unique_ptr<int> a = make_unique<int>(5); unique_ptr<int> b = std::move(a); cout << (a == nullptr) << *b;

Correct answer A. 15

Explanation

unique_ptr cannot be copied, but it can be moved. Moving transfers ownership of the int to b and leaves a guaranteed empty (null), so a == nullptr is true and prints 1, and *b is 5. Writing unique_ptr b = a without std::move would not compile because the copy constructor is deleted.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.