From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wm1-x32d.google.com (mail-wm1-x32d.google.com [IPv6:2a00:1450:4864:20::32d]) by sourceware.org (Postfix) with ESMTPS id A6C883858002 for ; Wed, 22 Jun 2022 11:51:30 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org A6C883858002 Received: by mail-wm1-x32d.google.com with SMTP id h14-20020a1ccc0e000000b0039eff745c53so4662300wmb.5 for ; Wed, 22 Jun 2022 04:51:30 -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=W0fqejTaEH3S29yUcGPZOPKYtRtV9A3FfNG5UXR5PJ8=; b=z8rFjUMOX0UHjLuDyByrbrpQOz8Y0wm+uc/G2EikospWnM6WR41rLXArODY5oCwpFy 3tsS+n58jUFN+F4b3IOCI/Cfd+QTedJMFyFjV2KJTnieYJmya6JFpYncF3w8cRhUIAKr Y0muNbnHUJ17Lg+lljMmMoQtJiDkLpi9uGzfHl+6siFynh77G/20m5bWh2GZF6HAH3fX 4pa/dFlaKjyGYzKuCRDBhALYWAc7DVfQ4be0tnKbWk3nn6ZknetLi6jDnaeoVUv33Ped Lq5Nb7uzb5cjWIt73O1KucmGd7xRJXKXSHHdsc2RgG3dtHfiSkdDgTEfoTopIdHe70UK ESDQ== X-Gm-Message-State: AJIora+uyWGZd6gHh8qkwq6Z+qcCF3BscbVoodSzL3IojxB3xLjlZtNs ai3CM2T9mxVRLkR1aqfEnvuFqNnKK7VgXwyIdxk= X-Google-Smtp-Source: AGRyM1sox/pC/Zn5zOr6Mx2oe+QzFjXtPqkDLfp0XORlQQWMLyXYekJu2+bDZJxvh4mx6e5FXFrA7TpTiMKkC+S0KV0= X-Received: by 2002:a05:600c:34d0:b0:3a0:2c07:73ac with SMTP id d16-20020a05600c34d000b003a02c0773acmr1523144wmq.85.1655898689447; Wed, 22 Jun 2022 04:51:29 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Jonathan Wakely Date: Wed, 22 Jun 2022 12:51:18 +0100 Message-ID: Subject: Re: Passing member variable as parameter to the base class constructor: warning expected but not seen To: Ronny Meeus Cc: gcc-help Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-1.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 11:51:32 -0000 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.