[Bug c++/124197] template-for vs -Wshadow

hello at bjornpagen dot com via Gcc-bugs <[email protected]>
Newsgroups gmane.comp.gcc.bugs
Message-ID <[email protected]/bugzilla/>
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=124197

Bjorn Pagen <hello at bjornpagen dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |hello at bjornpagen dot com

--- Comment #1 from Bjorn Pagen <hello at bjornpagen dot com> ---
I confirmed this behavior with GCC 16.1.0 and the C++26 reflection
implementation.

The reflection form also reports -Wshadow for the expansion variable:

#include <meta>
#include <cstddef>
#include <string_view>

enum class Compass { North, East, South, West };

consteval auto count() -> std::size_t {
  auto result = std::size_t{0};
  template for (constexpr auto enumerator
                : std::define_static_array(
                      std::meta::enumerators_of(^^Compass))) {
    if (!std::meta::identifier_of(enumerator).empty())
      ++result;
  }
  return result;
}

static_assert(count() == 4);

Command:

g++-16 -std=c++26 -freflection -Wshadow -Werror -c repro.cc

GCC reports four -Wshadow errors at the declaration of enumerator. It reports
one error for each expanded element.

The source contains one variable declaration and no nested declaration with the
same name. The warnings appear to come from the scopes that GCC creates for
expansion elements.

This behavior forces projects that use -Werror to disable -Wshadow for each
translation unit that contains an expansion statement.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.