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).
Seems like a compiler bug to me. The attribute can be applied to any variable declaration and that's a variable declaration.
Filed 91304.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With