Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

[[maybe_unused]] in if statement doesn't seem to work in GCC

The whole point in [[maybe_unused]] is that it tells the compiler to not warn for unused variables, right? So why does GCC warn that ui is unused, here?

#include <iostream>
#include <vector>

#define NDEBUG
#include <cassert>

int main()
{   
  std::vector<int> numbers = {1,2,3};

  if ([[maybe_unused]] auto  ui = static_cast<unsigned int>(numbers.size()))
  {
    assert(ui);
    std::cout << "ok" << std::endl;
  }
}

Am I doing something wrong? Is this a bug in the compiler?

(As an aside: trying this on Wandbox, Clang doesn't seem to warn that ui is unused regardless of whether or not the [[maybe_unused]] is present).

(Further aside: MSVC also does not warn that ui is unused, even if [[maybe_unused]] is not present).

like image 622
Len Avatar asked Nov 23 '25 02:11

Len


1 Answers

Seems like a compiler bug to me. The attribute can be applied to any variable declaration and that's a variable declaration.

Filed 91304.

like image 102
Barry Avatar answered Nov 25 '25 16:11

Barry