这是以一种有趣的方式指定的,它看起来像MSVC在此处对字母实施了标准。分配本身是在§20.8.2.2.3[util.smartptr.shared.assign]/P1-3指定:
shared_ptr& operator=(const shared_ptr& r) noexcept;
template<class Y> shared_ptr& operator=(const shared_ptr<Y>& r) noexcept;
template<class Y> shared_ptr& operator=(auto_ptr<Y>&& r);
影响:等价于shared_ptr(r).swap(*this)
。
返回:*this
。
[注:由临时对象的构造和销毁的使用计数更新不是 可观察到的副作用,因此该实施方式可以达到经由 不同装置的影响(以及隐含的保证),而不创建临时。 <example omitted>
]
相关构造在§20.8.2.2.1[util.smartptr.shared.const]/p17-19指定:
shared_ptr(const shared_ptr& r) noexcept;
template<class Y> shared_ptr(const shared_ptr<Y>& r) noexcept;
需要:第二个构造除非Y*
可以隐式转换为T*
,否则不得参与重载决议。
影响:如果r
是空的,构造一个空shared_ptr
对象;否则,构建与 r
共享所有权的shared_ptr
对象。
后续条件:get() == r.get() && use_count() == r.use_count()
。
由于const TestClass *
不隐式转换为TestClass *
,模板化的构造函数不参与重载解析,渲染shared_ptr(r)
病态的,因为没有匹配的构造函数。
编辑:我看到了混乱。报告编译器错误消息时,VS2012的设计相当糟糕。由编译器所发出的完整的错误信息是:
error C2440: '<function-style-cast>' : cannot convert from 'const std::shared_ptr<_Ty>' to 'std::shared_ptr<_Ty>'
with
[
_Ty=const TestClass
]
and
[
_Ty=TestClass
]
重要的是,这两个_Ty
S IN误差输出指的是不同类型。然而,VS2012中的错误列表窗口仅将其截断到第一行,并丢失了重要信息。您应该查看完整的错误消息的构建输出。
[本列表中的第二个](http://en.cppreference.com/w/cpp/memory/shared_ptr/operator%3D)(仍然标为#1)。 –