Printing unique_ptr to cout

Printing unique_ptr to cout

本文关键字:to cout ptr unique Printing      更新时间:2023-10-16

无法理解为什么会失败?

int *p = new int(10);
std::unique_ptr<int> ptr(p);
// Below line gives compilation error.
std::cout << "Value of ptr        " << ptr << std::endl;
// Below line works well.
std::cout << "Value pointed ptr   " << *ptr << std::endl;
std::cout << "Value of ptr->get() " << ptr.get() << std::endl;

我是这样理解的:

假设 p 的地址是

100,新分配的内存的地址是 200。

p                new allocated memory
----------       ---------
   200              10
----------       ---------
100              200

ptr
----------
   200
----------
300

在上面的描述中,unique_ptr指向新分配的内存本身,避免了"p"。那么,打印"ptr"不应该给我 200 吗?

std::unique_ptr<int> ptr(p);
// Below line gives compilation error.
std::cout << "Value of ptr        " << ptr << std::endl;

为了能够使用通常的<<语法来打印使用cout的某个类的对象,必须有一个适当的重载operator<<实现。

例如,如果你有一个类 X,如果你想启用 cout << x 语法,你可以像这样重载operator<<

#include <ostream> // for std::ostream
std::ostream& operator<<(std::ostream& os, const X& x)
{
  // Implement your output logic for 'x'
  ...
  return os;
}

C++标准库设计者选择不为std::unique_ptr实现这样的重载;这就是为什么当你尝试将<<unique_ptr的实例一起使用时会出现编译错误。

那么,打印"ptr"不应该给我 200 吗?

如果指定的标准库std::unique_ptr应该可以流式传输到标准流中,则应该这样做。换句话说,应该存在std::unique_ptr operator <<超载。

但是,该标准没有指定这样的事情,因此流式传输unique_ptr会导致编译错误(没有operator <<接受它(。解决方案就像你发现的那样:如果你需要流式传输指针,请获取指针:

stream << ptr.get()

问题是在 C++20 之前,unique_ptr没有重载operator<<

但从C++20开始,有的,从unique_ptr可以看出:

template< class CharT, class Traits, class Y, class D >
std::basic_ostream<CharT, Traits>& operator<<( std::basic_ostream<CharT, Traits>& os, const std::unique_ptr<Y, D>& p );

p管理的指针的值插入到输出流os中。

相当于os << p.get().

这意味着,std::cout << ptr;将在 C++20(及以后(中工作。

演示