When I compile the following code, I get a warning (https://godbolt.org/z/Tx7v6jWf1):
void foo() {
    int _;
    // warning: name-independent declarations only available with
    //           '-std=c++2c' or '-std=gnu++2c' [-Wc++26-extensions]
    int _;
}
What exactly has changed about _ variables in C++26, and what's a name-indepent declaration?
                        
P2169: A nice placeholder with no name has been accepted into C++26, which makes
_special in the following contexts:int _)auto [x, _])[_ = 0] {})struct S { int _; })In such contexts,
_makes the declaration name-independent._suppresses warningsThe standard says:
This is very similar to the recommendation for
[[maybe_unused]]in [dcl.attr.unused] p4. Normally, you would get a warning for unused variables (-Wunusedin GCC), but_and[[maybe_unused]]suppress this.Historically, developers have used
_as a "placeholder" for unused things, so this is just standardizing existing practice._can be declared multiple timesFurthermore, name-independent declarations cannot potentially conflict. In short, you can declare
_multiple times. However, name lookup cannot be ambiguous.This code is taken from [basic.scope.scope] example 3.
Note that
_also has some special interactions withusingdeclarations. See [namespace.udecl] p10 for more details.Two
_are not the same entity from the linker's perspectiveEven with external linkage, two
_are not considered the same entity:When linked, this program is OK. For any name other than
_, this would give you a "multiple definitions" linker error. See also [basic.link] p8.Compiler Support
Only GCC 14 and Clang 18 support this feature at the time of writing. See C++26 compiler support for more.
If you need to test for support, test for
__cpp_placeholder_variables: