From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 87395 invoked by alias); 29 Aug 2019 10:07:33 -0000 Mailing-List: contact gcc-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-owner@gcc.gnu.org Received: (qmail 87381 invoked by uid 89); 29 Aug 2019 10:07:33 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-0.5 required=5.0 tests=AWL,BAYES_50,FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,SPF_PASS autolearn=ham version=3.3.1 spammy=privately, inaccessible, schneider, Schneider X-HELO: mail-wm1-f65.google.com Received: from mail-wm1-f65.google.com (HELO mail-wm1-f65.google.com) (209.85.128.65) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Thu, 29 Aug 2019 10:07:31 +0000 Received: by mail-wm1-f65.google.com with SMTP id f72so3042572wmf.5 for ; Thu, 29 Aug 2019 03:07:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=STWKVIoJ8fatyPhMCYUAvGARBnG5HEXwUIRN/2jBaz4=; b=HaST1fBlZF1EZ0uqilI8JbGLIH1FWPkAYI8peBl3G846/3G5nZxooxIWEXcRRoL2v0 aNRNZIxKboEsUh6TM+DqjPXx/zQPgmneLwlop8pY9dy6DvPOdNrwCQk+PRpl4OISwcAc KXrGD895wmoe8hAmQgclxi4v18HOHr8tXFpNGK3GPPQXYV8fcQoZgxn7QJ3Rdrhbiud1 Iex9hFYId9Rc4Fkzr9hFYYZK7wDFw95Pmz5KY2nBM4VFOJfpS6ESLwsRn8t4xmrptbBj 91D+0XjYe7m1J88x5qLX+ijbKHQw+C9qmeXluMhrS2yR5Udt0WvNe99fRdnJEUSYIgzB y8gw== MIME-Version: 1.0 References: <271ebc76-2e27-d4b4-6cd6-9f7a5c8ff7a1@radiodata.biz> In-Reply-To: <271ebc76-2e27-d4b4-6cd6-9f7a5c8ff7a1@radiodata.biz> From: Jonathan Wakely Date: Thu, 29 Aug 2019 10:07:00 -0000 Message-ID: Subject: Re: enable_shared_from_this fails at runtime when inherited privately To: Christian Schneider Cc: llvm-dev@lists.llvm.org, "gcc@gcc.gnu.org" , christian@ch-sc.de, premmers@radiodata.biz Content-Type: text/plain; charset="UTF-8" X-IsSubscribed: yes X-SW-Source: 2019-08/txt/msg00224.txt.bz2 On Thu, 29 Aug 2019 at 10:15, Christian Schneider wrote: > > 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 > #include > #include > #include > > #ifndef prefix > #define prefix std > #endif > > class foo: > prefix::enable_shared_from_this > { > public: > prefix::shared_ptr get_sptr() > { > return shared_from_this(); > } > }; > > int main() > { > auto a = prefix::make_shared(); > 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. It definitely is a requirement of the standard. The new wording we added via http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0033r1.html#spec says that the base's weak_ptr is only initialized when the base class is "unambiguous and accessible". It doesn't say that an ambiguous or inaccessible base class makes the program ill-formed, so we're not allowed to reject such a program. > 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' is an inaccessible base of 'foo'; > clang: error: cannot cast 'boost::shared_ptr::element_type' (aka > 'foo') to its private base class 'boost::enable_shared_from_this') That seems like a bug in Boost. > 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. No, that would not conform to the standard.