std::unique_ptr<int> ptr(p);
To use the usual << syntax to print an object of a class using cout , the proper operator<< overload must be implemented.
For example, if you have class X, if you want to include the syntax cout << x , you can overload operator<< as follows:
#include <ostream> // for std::ostream std::ostream& operator<<(std::ostream& os, const X& x) { // Implement your output logic for 'x' ... return os; }
The designers of the C ++ standard library decided not to implement such an overload for std::unique_ptr ; therefore, you get a compilation error when you try to use << with unique_ptr s instances.
source share