Search code examples
c++gccc++17unused-variablesstructured-bindings

structured bindings and range-based-for; supress unused warning in gcc


I want to traverse a map using structure bindings, ignoring the key:

for (auto& [unused, val] : my_map)
     do_something(val);

I have tried different options with gcc-7.2.0:

// The warning is issued
for ([[maybe_unused]] auto& [unused, val] : my_map)
      do_something(val);

// Syntax error
for (auto& [[[maybe_unused]] unused, val] : my_map)
      do_something(val);

// The same two combinations above with [[gnu::unused]].

It seems that the [[maybe_unused]] attribute is not implemented yet for structure bindings.

Is there any simple solution to this? Any macro, gcc/gnu extension, or any pragma to temporarily supress that specific warning is fine to me; for example, disabling it during the whole function body where I'm using the range-based-for, because the function I'm using this is pretty short (it is basically two range-for-loops over two different maps with exact behaviour).

The (relevant) options I'm using to compile the project are:

-std=c++17 -Wall -Wextra -Werror -pedantic -pedantic-errors

What I have current to go on is, but that's ugly:

for (auto& [unused, val] : my_map)
   (void)unused, do_something(val);

Solution

  • The relevant GCC pragmas are documented on this page.

    #include <map>
    
    std::map<int, int> my_map;
    
    void do_something(int);
    
    void loop()
    {
    #pragma GCC diagnostic push
    #pragma GCC diagnostic ignored "-Wunused-variable"
        for (auto & [unused, val]: my_map)
    #pragma GCC diagnostic pop
            do_something(val);
    
    }
    

    This is the smallest scope of the disabled warning that I could have, and still have the warning suppressed with -Wall -Wextra -Werror.