Counting Copies
Test your C++ programming skills by solving the given puzzle about copy elision and return value optimization.
We'll cover the following
Puzzle code
Carefully read the code given below:
#include <iostream>struct Resource{Resource() = default;Resource(const Resource &other){std::cout << "copy\n";}};Resource getResource(){return Resource{};}int main(){Resource resource1 = getResource();Resource resource2{resource1};}
Your task: Guess the output
Try to guess the output of the above code. Attempt the following quiz to assess your understanding.
Q
What is the expected output of the above code?
A)
copy
B)
copy
copy
C)
Undefined behavior
D)
Compilation error
Let’s discuss this code and output together in the next lesson.
Level up your interview prep. Join Educative to access 70+ hands-on prep courses.