给出以下源代码:
#include <memory>
#include <iostream>
using namespace std;
struct concept
{
virtual void perform() = 0;
};
struct model : concept, enable_shared_from_this<model>
{
void perform() override {
cout << "my pointer is " << shared_from_this().get() << endl;
}
};
int main(int argc, const char * argv[])
{
// shared_ptr<concept> concept_ptr = make_shared<model>();
shared_ptr<concept> concept_ptr { new model };
concept_ptr->perform();
return 0;
}
在gcc
下编译,此代码编译并关联内部weak_ptr
地址为model
.
在 clang
下代码将无法编译(错误信息包含在末尾)
如果你替换 concept_ptr
的初始化与 shared_ptr<concept> concept_ptr = make_shared<model>();
它将在两者上编译。
哪个是正确的?
编辑:
我的 clang 版本是 Xcode
附带的版本。 :
$ clang --version
Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)
Target: x86_64-apple-darwin13.3.0
Thread model: posix
编辑2:
只是想感谢大家的贡献。
如果您有兴趣,我想这样做的原因是我想要一个不透明的接口(interface)来实现具有共享句柄语义的实现。一些实现(异步的)要求回调对象确保实现对象仍然存在(争论 shared_from_this
和 weak_ptr::lock
)。其他实现不需要这个。我想避免使用 enable_shared_from_this<>
妨碍概念(公共(public)接口(interface))基类,因为它将实现与接口(interface)结合在一起 - 一个已知的邪恶。
在大多数情况下,使用 make_shared 创建实现对象是合理的。在需要自定义析构函数的极少数情况下,以下内容似乎是可移植的:
auto concept_ptr = static_pointer_cast<concept>(shared_ptr<model> {
new model ,
[](model* self) {
// some_deletion_operation on self;
} });
附录: 关于 clang 的错误消息:
In file included from /Users/richardh/Documents/dev/Scratchpad/tryit/tryit/try2.cpp:1:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/memory:4013:35: error: no viable overloaded '='
__e->__weak_this_ = *this;
~~~~~~~~~~~~~~~~~ ^ ~~~~~
...etc...
最佳答案
我知道 libstdc++ 在这里更严格地遵循标准。
关于要求
shared_ptr<T> shared_from_this();
shared_ptr<const T> shared_from_this() const;
N3337 §20.7.2.4 (7) 和 N3936 §20.8.2.5 (7) 都只需要
enable_shared_from_this<T>
shall be an accessible base class ofT
.*this
shall be a subobject of an objectt
of typeT
. There shall be at least oneshared_ptr
instancep
that owns&t
.
没有要求名为 shared_ptr
拥有&t
实际上必须是 shared_ptr<T>
或 shared_ptr<A_to_T_Convertible>
.
而这个功能正是该类功能的核心。
因此,给定 Tp
作为 enabled_shared_from_this
的实际参数和 Tp1
作为拥有shared_ptr
的实际参数, is_convertible<Tp1, Tp>::value == true
,更不用说is_same<Tp1, Tp>::value == true
, 不是标准要求的,对于各个指针都是一样的。
确实,使用 libc++ 的 clang++ 的完整输出具有
/usr/local/bin/../include/c++/v1/memory:3997:35: error: no viable overloaded '='
__e->__weak_this_ = *this;
~~~~~~~~~~~~~~~~~ ^ ~~~~~
/usr/local/bin/../include/c++/v1/memory:4035:5: note: in instantiation of
function template specialization
'std::__1::shared_ptr<concept>::__enable_weak_this<model>' requested here
__enable_weak_this(__p);
^
[...]enable_shared.cxx:34:25: note: in instantiation
of function template specialization
'std::__1::shared_ptr<concept>::shared_ptr<model>' requested here
shared_ptr<concept> model_ptr1(new model);
^
/usr/local/bin/../include/c++/v1/memory:4942:15: note: candidate function not
viable: no known conversion from 'std::__1::shared_ptr<concept>' to 'const
std::__1::weak_ptr<model>' for 1st argument
weak_ptr& operator=(weak_ptr const& __r) _NOEXCEPT;
^
/usr/local/bin/../include/c++/v1/memory:4953:15: note: candidate function not
viable: no known conversion from 'std::__1::shared_ptr<concept>' to
'std::__1::weak_ptr<model>' for 1st argument
weak_ptr& operator=(weak_ptr&& __r) _NOEXCEPT;
^
/usr/local/bin/../include/c++/v1/memory:4949:9: note: candidate template
ignored: could not match 'weak_ptr' against 'shared_ptr'
operator=(weak_ptr<_Yp> const& __r) _NOEXCEPT;
^
/usr/local/bin/../include/c++/v1/memory:4960:9: note: candidate template
ignored: could not match 'weak_ptr' against 'shared_ptr'
operator=(weak_ptr<_Yp>&& __r) _NOEXCEPT;
^
/usr/local/bin/../include/c++/v1/memory:4967:13: note: candidate template
ignored: disabled by 'enable_if' [with _Yp = concept]
is_convertible<_Yp*, element_type*>::value,
^
所以libc++这里要
is_convertible<Tp1* /*= Base* = concept**/, Tp* /*= Derived* = model* */>
这里当然失败了,运行时*this
非常shared_ptr<Tp1>
将是 dynamic_cast
-能够Tp*
这里没有ansatz。
从这个角度来看,shared_ptr<concept> concept_ptr = make_shared<model>();
的原因也很清楚。不会因此而受苦;在 rhs
有一个shared_ptr<Tp /* = derived = model */>
构造函数并为此 ptr
is_convertible
持有。
libstdc++ 不受此影响,因为它传递了 参数,因此它的类型(= Derived = model)为 shared_ptr<Tp1 /* = Base = concept*/>
构造函数到内部 weak_ptr<T /*= Derived = model*/>
分配,而不是 shared_ptr
正在 build 中。
https://github.com/mirrors/gcc/blob/master/libstdc%2B%2B-v3/include/bits/shared_ptr_base.h#L848
template<typename _Tp, _Lock_policy _Lp>
class __shared_ptr
{
https://github.com/mirrors/gcc/blob/master/libstdc%2B%2B-v3/include/bits/shared_ptr_base.h#L858
template<typename _Tp1>
explicit __shared_ptr(_Tp1* __p)
: _M_ptr(__p), _M_refcount(__p)
{
__glibcxx_function_requires(_ConvertibleConcept<_Tp1*, _Tp*>)
static_assert( !is_void<_Tp1>::value, "incomplete type" );
static_assert( sizeof(_Tp1) > 0, "incomplete type" );
__enable_shared_from_this_helper(_M_refcount, __p, __p);
}
https://github.com/mirrors/gcc/blob/master/libstdc%2B%2B-v3/include/bits/shared_ptr_base.h#L1459
template<typename _Tp, _Lock_policy _Lp>
class __enable_shared_from_this
{
https://github.com/mirrors/gcc/blob/master/libstdc%2B%2B-v3/include/bits/shared_ptr_base.h#L1482
private:
template<typename _Tp1>
void
_M_weak_assign(_Tp1* __p, const __shared_count<_Lp>& __n) const noexcept
{ _M_weak_this._M_assign(__p, __n); }
template<typename _Tp1>
friend void
__enable_shared_from_this_helper(const __shared_count<_Lp>& __pn,
const __enable_shared_from_this* __pe,
const _Tp1* __px) noexcept
{
if (__pe != 0)
__pe->_M_weak_assign(const_cast<_Tp1*>(__px), __pn);
}
仅代表我的观点;欢迎评论。
@Richard Hodges:+1,非常有趣的话题
关于c++ - 根据标准,这些编译器中的哪个有错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24851110/