为什么shared_ptr< T> :: use_count()返回long而不是unsigned类型? [英] Why does shared_ptr<T>::use_count() return a long instead of an unsigned type?
问题描述
shared_ptr观察者20.8.2.2.5 C ++ 14最终草案(n4296)
shared_ptr observers 20.8.2.2.5 C++14 Final Draft (n4296)
long use_count() const noexcept;
返回:与 * this
共享所有权的 shared_ptr
对象的数量,包括 * this
,如果 *,则为0这个
是空的.
Returns: the number of shared_ptr
objects, *this
included, that share ownership with *this
, or 0 when *this
is empty.
[注意: use_count()
不一定有效.—尾注]
[Note: use_count()
is not necessarily efficient. — end note]
推荐答案
根据此页面
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2003/n1450.html
对use_count的返回类型进行签名,以避免诸如此类的陷阱p.use_count()> -1评估为false.
The return type of use_count is signed to avoid pitfalls such as p.use_count() > -1 evaluating to false.
参考
John Lakos,大规模C ++软件设计,第9.2.2节,第637页,Addison-Wesley,1996年7月,ISBN 0-201-63362-0.
John Lakos, Large-Scale C++ Software Design, section 9.2.2, page 637, Addison-Wesley, July 1996, ISBN 0-201-63362-0.
基本上,这看起来像是保姆级的决定,是为新生软件开发人员量身定制的.
Basically, it looks like a nanny-grade decision, tailored for freshman year software developers.
这篇关于为什么shared_ptr< T> :: use_count()返回long而不是unsigned类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!