From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 520 invoked by alias); 29 Aug 2019 09:15:28 -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 119048 invoked by uid 89); 29 Aug 2019 09:15:21 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-1.2 required=5.0 tests=BAYES_00,KAM_INFOUSMEBIZ,SPF_PASS autolearn=no version=3.3.1 spammy=H*F:D*biz, H*M:biz, H*MI:biz, H*r:192.168.2 X-HELO: smtp.radiodata.biz Received: from smtp.radiodata.biz (HELO smtp.radiodata.biz) (116.203.112.52) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Thu, 29 Aug 2019 09:15:18 +0000 Received: from localhost (localhost [127.0.0.1]) by smtp.radiodata.biz (Postfix) with ESMTP id 9811A3E8F2; Thu, 29 Aug 2019 11:15:16 +0200 (CEST) Received: from smtp.radiodata.biz ([116.203.112.52]) by localhost (smtp.radiodata.biz [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id TqtQVjjd4sBD; Thu, 29 Aug 2019 11:15:09 +0200 (CEST) Received: from mail.radiodata.biz (p578044f8.dip0.t-ipconnect.de [87.128.68.248]) by smtp.radiodata.biz (Postfix) with ESMTPSA id 7F9903E8E6; Thu, 29 Aug 2019 11:15:09 +0200 (CEST) Received: from [192.168.2.143] (radiobox-gentoo.radiodata.xx [192.168.2.143]) by mail.radiodata.biz (Postfix) with ESMTPSA id 2B3E820323; Thu, 29 Aug 2019 11:15:09 +0200 (CEST) To: llvm-dev@lists.llvm.org, gcc@gcc.gnu.org From: Christian Schneider Subject: enable_shared_from_this fails at runtime when inherited privately Cc: christian@ch-sc.de, premmers@radiodata.biz Message-ID: <271ebc76-2e27-d4b4-6cd6-9f7a5c8ff7a1@radiodata.biz> Date: Thu, 29 Aug 2019 09:15:00 -0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.8.0 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-SW-Source: 2019-08/txt/msg00223.txt.bz2 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. 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') 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))