public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* enable_shared_from_this fails at runtime when inherited privately
@ 2019-08-29  9:15 Christian Schneider
  2019-08-29 10:07 ` Jonathan Wakely
  0 siblings, 1 reply; 6+ messages in thread
From: Christian Schneider @ 2019-08-29  9:15 UTC (permalink / raw)
  To: llvm-dev, gcc; +Cc: christian, premmers

Hello,
I just discovered, that, when using enable_shared_from_this and 
inheriting it privately, this fails at runtime.
I made a small example:

#include <memory>
#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>
#include <boost/enable_shared_from_this.hpp>

#ifndef prefix
#define prefix std
#endif

class foo:
     prefix::enable_shared_from_this<foo>
{
public:
     prefix::shared_ptr<foo> get_sptr()
     {
         return shared_from_this();
     }
};

int main()
{
     auto a = prefix::make_shared<foo>();
     auto b = a->get_sptr();
     return 0;
}

This compiles fine, but throws a weak_ptr exception at runtime.
I'm aware, that the implementation requires, that 
enable_shared_from_this needs to be publicly inherited, but as a first 
time user, I had to find this out the hard way, as documentations (I 
use, ie. cppreference.com) don't mention it, probably because it's not a 
requirement of the standard.

On the other hand, if you compile the code with additional 
-Dprefix=boost (and needed boost stuff installed, of course), it gives a 
compiler error (
gcc: 'boost::enable_shared_from_this<foo>' is an inaccessible base of 'foo';
clang: error: cannot cast 'boost::shared_ptr<foo>::element_type' (aka 
'foo') to its private base class 'boost::enable_shared_from_this<foo>')

I'm think, it would be helpful, if the std implemantions also would fail 
at compile time already, and wanted to ask if this would be 
possible/feasible.

BR, Christian

compilers:
gcc-Version 9.2.0 (Gentoo 9.2.0 p1)
clang version 8.0.1 (tags/RELEASE_801/final) (used with both 
libstdc++.so.6 and libc++.so.1 (v8.0.1))

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2019-08-29 14:46 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-08-29  9:15 enable_shared_from_this fails at runtime when inherited privately Christian Schneider
2019-08-29 10:07 ` Jonathan Wakely
2019-08-29 10:50   ` Christian Schneider
2019-08-29 11:43     ` Jonathan Wakely
2019-08-29 11:45       ` Jonathan Wakely
2019-08-29 14:46         ` Christian Schneider

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).