c++17 introduces a nice syntax for binding pairs/tuples to individual values. Especially when iterating a map, this makes it easier to assign variable names to both the key and value. E.g.,
for( auto const [ key, value ] : my_map )
{
foo( value );
}
However, when I loop through, I don't always need to use the key, in which case I get unused variable warnings on key
.
What is the cleanest way to suppress the warnings from this context (but not others), using the gcc compiler? If I pre-declare key
and value
prior to the loop, then I cannot make them const
. Is there some nicer approach than casting key
to void inside the loop---but that still retains the [ key, value ]
syntax?