Given the following source code:
#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;
}
Compiling under gcc
, this code compiles and associates the internal weak_ptr
with the address of model
.
Under clang
the code will not compile (error message included at the end)
If you replace the initialisation of concept_ptr
with shared_ptr<concept> concept_ptr = make_shared<model>();
it will compile on both.
Which is correct?
edit:
My version of clang is the one that ships with 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
edit2:
Just wanted to say thanks to everyone for contributing.
If you're interested, the reason I want to do this is that I want an opaque interface to an implementation with shared-handle semantics. Some implementations (async ones) require that callback objects ensure that the implementation object still exists (argues for shared_from_this
and weak_ptr::lock
). Other implementations do not require this. I wanted to avoid encumbering the concept (public interface) with the enable_shared_from_this<>
base class, since that couples implementation with interface - a known evil.
In most cases, it's reasonable to use make_shared to create the implementation object. In rarer cases that require a custom destructor, the following seems portable:
auto concept_ptr = static_pointer_cast<concept>(shared_ptr<model> {
new model ,
[](model* self) {
// some_deletion_operation on self;
} });
appendix: error message on 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...
I understand that libstdc++ follows the standard more closely here.
Concerning the requirements for
both N3337 §20.7.2.4 (7) and N3936 §20.8.2.5 (7) only require
There is no requirement named that one
shared_ptr
owning&t
actually has to be ashared_ptr<T>
orshared_ptr<A_to_T_Convertible>
.And that very function is the core of that class' functionality.
Thus, given
Tp
as the actual param of theenabled_shared_from_this
andTp1
as the actual parameter of that owningshared_ptr
,is_convertible<Tp1, Tp>::value == true
, let aloneis_same<Tp1, Tp>::value == true
, is not required by the standard, same for respective pointers.And indeed, the full output of clang++ using libc++ has
So libc++ here wants
which of course fails here, that the run-time
*this
of that veryshared_ptr<Tp1>
would bedynamic_cast
-able toTp*
is out of ansatz here.From this perspective, it's also clear why
shared_ptr<concept> concept_ptr = make_shared<model>();
doesn't suffer from that; on therhs
there is ashared_ptr<Tp /* = derived = model */>
constructor and for thatptr
is_convertible
holds.libstdc++ doesn't suffer from this, because it passes the argument, thus its type (= Derived = model), of the
shared_ptr<Tp1 /* = Base = concept*/>
constructor down to the internalweak_ptr<T /*= Derived = model*/>
assignment, not theshared_ptr
in construction.https://github.com/mirrors/gcc/blob/master/libstdc%2B%2B-v3/include/bits/shared_ptr_base.h#L848
https://github.com/mirrors/gcc/blob/master/libstdc%2B%2B-v3/include/bits/shared_ptr_base.h#L858
https://github.com/mirrors/gcc/blob/master/libstdc%2B%2B-v3/include/bits/shared_ptr_base.h#L1459
https://github.com/mirrors/gcc/blob/master/libstdc%2B%2B-v3/include/bits/shared_ptr_base.h#L1482
My point of view only; comments welcome.
@Richard Hodges: +1, very interesting topic