Warnings for partially-unused structured bindings in C++26 mode

Ionuț Nicula via Gcc <[email protected]>
Newsgroups gmane.comp.gcc.devel
Message-ID <IwntDp_Q1VxRwhn7aMzNxnY2SVNNCEI8BMw2bMGfoymG74jx1ONjefmAtoNwjrkk2xcpHC7nBhKor_UBDinXr4qGnWi5pbKCkaK4G1RRzfQ=@nicula.xyz>
Is there any particular reason for not providing warnings for unused
structured bindings when at least one of the sub-objects is used?

For example:

    int foo() {
        auto [a, b, c] = std::make_tuple(1, 2, 3);
        return c;
    }

Here, even though `a` and `b` aren't used, you won't receive warnings
for them.

If GCC *did* provide warnings for them, this would be a bit awkward
pre-C++26 since `_` is not a special variable name so you can't use it
multiple times within the same scope, and having multiple unique names
but with `[[maybe_unused]]` would also be unergonomic.

But after the introduction of 'placeholder variables with no name' in
C++26 it's trivial (and ergonomic) to mark the first 2 sub-objects as
unused. You just do:

    int foo() {
        auto [_, _, c] = std::make_tuple(1, 2, 3);
        return c;
    }

So, given this, is there a reason to not make `-Wunused` in C++26 mode
provide warnings for `auto [a, b, c] = std::make_tuple(1, 2, 3)` if `a`
and `b` aren't used?

One of the related mistakes I've made before is iterating over an
associative container and accidentally not using the correct variable
for the key due to some copy-paste error or due to an unrelated variable
having a similar name. Something along the lines of:

    for (int i = 0; i < len; ++i) {
        // ...
        for (const auto& [id, value] : map[i]) {
            printf("key=%d value=%d\n", i, value);
        }
    }

Since `value` is used, an unused warning won't be issued for `id` and
therefore the code might look correct at first glance. A warning for
these kinds of issues would be pretty helpful, in my opinion, but let me
know what you folks think.
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.