Question: 4. a) Why is the destructor called 3 times? b) On which row will the Token object be created, which will disappear first? c) Why

4. a) Why is the destructor called 3 times? b) On which row will the Token object be created, which will disappear first? c) Why does this object disappear before the other two objects? d) Why is the default constructor called only once?

1 #include 2 #include 34 class Token { 5 public: 6 Token() { std::cout << "Token()"; } 7 ~Token() { std::cout << "~Token()"; } 8 }; 9 10 template 11 std::unique_ptr foo(T t0) { 12 return std::unique_ptr(new T(t0)); 13 }; 14 15 int main() { 16 Token&& t = Token(); 17 18 auto ptr = foo(t); 19 20 return 0; 21 }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!