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.

Reply via email to