Ric*_*ges 26 c++ gcc clang c++11
给出以下源代码:
#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;
}
Run Code Online (Sandbox Code Playgroud)
编译下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
Run Code Online (Sandbox Code Playgroud)
EDIT2:
只是想感谢大家的贡献.如果您感兴趣,我想要这样做的原因是我想要一个带有共享句柄语义的实现的不透明接口.一些实现(异步的)要求回调对象确保实现对象仍然存在(争论shared_from_this
和weak_ptr::lock
).其他实现不需要这样.我想避免使用enable_shared_from_this<>
基类来阻碍概念(公共接口),因为它将实现与接口耦合 - 一种已知的恶魔.
在大多数情况下,使用make_shared创建实现对象是合理的.在极少数需要自定义析构函数的情况下,以下内容似乎是可移植的:
auto concept_ptr = static_pointer_cast<concept>(shared_ptr<model> {
new model ,
[](model* self) {
// some_deletion_operation on self;
} });
Run Code Online (Sandbox Code Playgroud)
附录: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...
Run Code Online (Sandbox Code Playgroud)
Sol*_*kar 11
我知道libstdc ++在这里更接近标准.
关于要求
shared_ptr<T> shared_from_this();
shared_ptr<const T> shared_from_this() const;
Run Code Online (Sandbox Code Playgroud)
N3337§20.7.2.4(7)和N3936§20.8.2.5(7)均仅要求
enable_shared_from_this<T>
应该是一个可访问的基类T
.*this
应该是t
类型对象的子对象T
.至少应有一个拥有的shared_ptr
实例.p
&t
没有要求命名一个shared_ptr
拥有&t
实际上必须是shared_ptr<T>
或shared_ptr<A_to_T_Convertible>
.
而这个功能是该类功能的核心.
因此,给定Tp
作为对实际PARAM 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,
^
Run Code Online (Sandbox Code Playgroud)
所以这里的libc ++需要
is_convertible<Tp1* /*= Base* = concept**/, Tp* /*= Derived* = model* */>
Run Code Online (Sandbox Code Playgroud)
这当然失败这里,运行时*this
的那个很shared_ptr<Tp1>
会dynamic_cast
-able到Tp*
超出拟设这里.
从这个角度来看,也很清楚为什么shared_ptr<concept> concept_ptr = make_shared<model>();
不受此影响; 在rhs
那里有一个shared_ptr<Tp /* = derived = model */>
构造函数,为此ptr
is_convertible
成立.
的libstdc ++不会从该患,因为它通过参数,从而其类型(=派生=模型),所述的shared_ptr<Tp1 /* = Base = concept*/>
构造下到内部weak_ptr<T /*= Derived = model*/>
分配,而不是shared_ptr
在结构.
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
{
Run Code Online (Sandbox Code Playgroud)
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);
}
Run Code Online (Sandbox Code Playgroud)
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
{
Run Code Online (Sandbox Code Playgroud)
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);
}
Run Code Online (Sandbox Code Playgroud)
我的观点只是; 欢迎评论.
@Richard Hodges:+1,非常有趣的话题