#include <memory>
#include <iostream>
class Token { public:
Token() { std::cout << "Token()"; }
~Token() { std::cout << "~Token()"; }
};
template <class T>
std::unique_ptr<T> foo(T t0) {
return std::unique_ptr<T>(new T(t0)); };
int main() {
Token&& t = Token();
auto ptr = foo<Token>(t);
return 0;
}
On which occasions will the destructor be called?
I think it will be called firstly when we call Token()
, it creates a temporary object which is destructed immediately, then in the foo()
function when t0
is destructed, and then when main()
ends, and ptr
goes out of scope.
But my friend says otherwise, so how will it actually be?