Suppress Compiler warning Function declared never referenced

Jtello picture Jtello · Jun 20, 2012 · Viewed 27.1k times · Source

So i have some code like this:

void foo (int, int);

void bar ( )
{
    //Do Stuff

   #if (IMPORTANT == 1)
       foo (1, 2);
   #endif

}

When doing a compile without "IMPORTANT" I get a compiler Warning that foo is defined and never referenced. Which got me thinking (that is the problem).

So to fix this i just added the same #if (IMPORTANT == 1) around the function definition etc... to remove the warning, and then I started to wonder if there was a different way to suppress that warning on that function. I was looking at "unused" GCC attrib and didn't know if functions had the same attribute i could set? Is there even another way to suppress it that suppresses that warning for only that function and not the file?

Answer

TartanLlama picture TartanLlama · Jan 9, 2017

In C++17 you can declare your function with [[maybe_unused]]:

[[maybe_unused]] void foo (int, int);

This will suppress the warning and is the correct, idiomatic way to express a possibly unused function in C++17.