From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-lf1-x12d.google.com (mail-lf1-x12d.google.com [IPv6:2a00:1450:4864:20::12d]) by sourceware.org (Postfix) with ESMTPS id 81F0A385803D for ; Wed, 22 Jun 2022 13:08:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 81F0A385803D Received: by mail-lf1-x12d.google.com with SMTP id g4so15298976lfv.9 for ; Wed, 22 Jun 2022 06:08:27 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=TWzb7VZVWTL5OEIp1ExmD1SjSkJ3kcr6PKte4OGG3mw=; b=HneE61O3E5X3EeY5UGqpY9lLOLmj7Nzg74zg+RNqQhalPVjCAMIkShOY0y4hCDkRyq OC94YHAKp6APGL571QDSmRIHtAkbolxj+zeBKlxGgE5B7iSZ1H70PjEeZ1x4bCH/oH6+ o9+rSV+nVxZsjr3dL9JR45Mw3AyFax/1HGGKsbsW6L3bM5LliiBKWCUC57FgTMts9c6X 0HcgrObD5ahgXm83o5LlUR2jWP9J2kHSk0Fwb8huRUlhCy2X3QUUy++fTDs40A/lYi4g a56eZKkF7ogRp+ReHfVFRnB6sxG0tedkRh2bFEIR/Yu8Y0qRe7jkhe7HbHjNp1YflIh5 z2FQ== X-Gm-Message-State: AJIora/wK4Ci2P0bcuXfs2DegUBi8wDoU3nTVlrj4Owo4e1EmyF16MbI gOxTb3FeQtl91ZwawVnevfBKovd/+AGY5b4stHY= X-Google-Smtp-Source: AGRyM1sZdmn1EvBEvBbHAT1Raxye5mg5LSqrtuTI3FUPh8FPU57R3ebzE/KV/dT/FGG6/X5aGubZS70FrJqGzunRsuk= X-Received: by 2002:a05:6512:2249:b0:478:f926:ad7 with SMTP id i9-20020a056512224900b00478f9260ad7mr2027457lfu.511.1655903305810; Wed, 22 Jun 2022 06:08:25 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Ronny Meeus Date: Wed, 22 Jun 2022 15:08:14 +0200 Message-ID: Subject: Re: Passing member variable as parameter to the base class constructor: warning expected but not seen To: Jonathan Wakely Cc: gcc-help Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=0.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-help@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-help mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 22 Jun 2022 13:08:29 -0000 Op wo 22 jun. 2022 om 13:51 schreef Jonathan Wakely : > > On Wed, 22 Jun 2022 at 12:30, Ronny Meeus via Gcc-help > wrote: > > > > Hello > > > > I have a small test program (see below) that uses a member variable > > (a) of a derived class (A) as a parameter when calling the constructor > > of the base class (A_base). > > In fact this is "wrong" code since the base class constructor is > > called with an un-initialized variable as input. > > > > I would expect that the compiler generates a warning/error for this > > since the behavior completely depends on the contents of the memory > > where the object was allocated from. > > In the example code I use a placement new to actually show that the > > value seen in the constructor of the base class is the value I used to > > fill the memory with. > > > > I tried it with different gcc versions but none of these are > > generating a warning/error for this while other tools like coverity or > > sonarqube just report the issue ... > > Is it expected that there is no warning generated or do I need to pass > > extra compiler options to generate this warning? > > > > I compile the program like this: > > $ g++ -Wall -Wextra -std=gnu++11 /tmp/a.cc > > You didn't say which version of GCC you're using, but GCC 12 warns > about this now: > > w.C: In constructor 'A::A()': > w.C:16:17: warning: member 'A::a' is used uninitialized [-Wuninitialized] > 16 | A(): A_base(a) { > | ^ > > Older versions do not warn. Thanks for the feedback. For completeness I tested with: 10.3.0 7.3.0 4.8.5 (I know it are rather old versions but we work on embedded targets) Best regards, Ronny