* Warnings for partially-unused structured bindings in C++26 mode
@ 2026-05-12 22:26 Ionuț Nicula
0 siblings, 0 replies; only message in thread
From: Ionuț Nicula @ 2026-05-12 22:26 UTC (permalink / raw)
To: gcc
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.
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2026-05-12 22:26 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-05-12 22:26 Warnings for partially-unused structured bindings in C++26 mode Ionuț Nicula
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).