[英]Why does std::shared_ptr provide operator<<?
std::shared_ptr
provides operator<<
which just writes out its address. std::shared_ptr
提供operator<<
,它只写出它的地址。 There is no operator>>
and only the address without the content is recorded. 没有
operator>>
并且仅记录没有内容的地址。 I am wondering in which cases it is useful. 我想知道它在哪些情况下是有用的。
Because it is a potentially useful thing that <<
does on a raw pointer. 因为
<<
对原始指针执行是一个潜在有用的事情。 It is safe, and it is what raw pointers do, and shared_ptr
is supposed to be used to replace raw pointers in some situations. 它是安全的,它是原始指针所做的,并且
shared_ptr
应该用于在某些情况下替换原始指针。
In comparsion, >>
would very rarely make sense. 相比之下,
>>
很少有意义。 Unlike a raw pointer, storing a pointer value in a shared pointer takes ownership of it. 与原始指针不同,将指针值存储在共享指针中需要拥有它。
I can some_stream >> raw_ptr
and unless I do something with ptr
nothing goes wrong; 我可以
some_stream >> raw_ptr
,除非我用ptr
做什么都不会出错; a bit strange, but nothing immediately breaks. 有点奇怪,但没有立刻打破。
Doing the same to shared_ptr
would only be safe in extremely esoteric situations. 对
shared_ptr
执行相同操作只会在极端深奥的情况下安全。 In those situations, having to take a step where we first populate a raw pointer then load it into the shared pointer doesn't seem like an overly burdensome requirement. 在这些情况下,必须采取我们首先填充原始指针然后将其加载到共享指针的步骤似乎不是一个过于繁琐的要求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.