Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

[[maybe_unused]] attribute not working

Tags:

c++

gcc

c++17

cmake

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?

like image 824
Anubis Avatar asked Jun 10 '18 14:06

Anubis


1 Answers

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.

like image 58
Andrey Starodubtsev Avatar answered Oct 12 '22 05:10

Andrey Starodubtsev