10

I am trying to ignore the unused parameter warning using the new c++17 attribute [[maybe_unused]], as below.

int main([[maybe_unused]] int argc, char** argv)
{
    //...
}

But I still get warning: unused parameter ‘argc’ [-Wunused-parameter] with the following additional warning.

warning: ‘maybe_unused’ attribute directive ignored [-Wattributes]

I'm using g++ (GCC) 7.2.0 with cmake-3.11.3. My compiler flags are as follows.

-std=c++17 -Wall -pedantic -Wextra -Weffc++

I remember using this attribute successfully before, but I have no idea why this is not working now. Could someone show what I am doing wrong here?

Beta
  • 96,650
  • 16
  • 149
  • 150
Anubis
  • 6,995
  • 14
  • 56
  • 87
  • 6
    This is C++. If you flat out don't use it just don't name it. – StoryTeller - Unslander Monica Jun 10 '18 at 14:58
  • 10
    That warning means that the compiler doesn't support `[[maybe_unused]]`. Works just fine on g++ 7.2 over here however: https://godbolt.org/g/7KnuDx Double check the compiler version, and the compiler flags that are actually used by the generated makefile. That said, as StoryTeller points out, `[[maybe_unused]]` is redundant in a function argument, since you can simply leave it unnamed instead. – eerorika Jun 10 '18 at 15:11
  • In current compilers the attribute works as expected: https://gcc.godbolt.org/z/csf4Ezvzb – Fedor Aug 08 '21 at 19:42

1 Answers1

1

You can suppress warning about unused variable this way:

int main(int /* argc */, char** argv)
{
    //...
}

or using following trick:

int main(int argc, char** argv)
{
    (void)argc;

    //...
}

In this case this code will work for earlier versions of C++ standard and even for pure C.

Andrey Starodubtsev
  • 5,139
  • 3
  • 32
  • 46