https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78147

            Bug ID: 78147
           Summary: The -Wshadow warning is too aggressive with
                    constructor parameters
           Product: gcc
           Version: 6.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: psmith at gnu dot org
  Target Milestone: ---

Created attachment 39918
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=39918&action=edit
Simple patch disables shadow warnings for ctor params

There's a very common idiom in C++, I've even seen coding standards that
require it, where the constructor arguments have the same name as the member
they initialize:

  class Foo
  {
      int foo;
  public:
      Foo(int foo) : foo(foo) {}
  };

Unfortunately -Wshadow warns for this case.

Clang has an extra warning flag, -Wshadow-field-in-constructor, which controls
this (from what I can tell).  By default in Clang, -Wshadow doesn't enable this
you need to separately enable it.

Attached below is a patch which permanently disables the warning for parameters
in constructors.  If this seems interesting/desirable to people I can see about
putting some effort into adding it as a separate warning option.  If we would
prefer GCC, unlike clang, enable it automatically with -Wshadow and allow
-Wno-shadow-field-in-constructor to disable it, that's OK with me.

In an ideal world we'd disable shadow warnings only for situations where the
parameter is used as a simple assignment in an initializer list, but that's
clearly a lot more work so I didn't do that.  If we could do that I'd even
recommend not having a separate shadow option to control it, and just
hardcoding -Wshadow to never warn about that situation.

Reply via email to