3

This question is relevant to c++17 and above, which introduces the [[maybe_unused]] attribute.

I can apply maybe_unused to an entire unpacked tuple like so:

for ([[maybe_unused]] auto &[key, weak] : connection_cache_)
{
  if (auto locked = lock(weak); locked)
  {
    locked->cancel();
  }
}

However, marking the unused key does not work (at least on gcc-8):

for (auto &[ [[maybe_unused]] key, weak] : connection_cache_)
{
  if (auto locked = lock(weak); locked)
  {
    locked->cancel();
  }
}

This leaves me the choice of either not using the attribute or using a no-op to satisfy ensure no warnings when compiling with -Wall -Wextra -pedantic which is the norm for my codebase:

for (auto &[key, weak] : connection_cache_)
{
  boost::ignore_unused(key);
  if (auto locked = lock(weak); locked)
  {
    locked->cancel();
  }
}

Am I out of options?

Richard Hodges
  • 68,278
  • 7
  • 90
  • 142

0 Answers0