0

our code has a line like:

#define UNREFERENCED_PARAMETER(_p) (void) (_p)

STATUS RequestHandler(Request *request)
{
    UNREFERENCED_PARAMETER(request);
    ...
    return 0;
}

I don't know what is the mean of UNREFERENCED_PARAMETER, why translate the 'request' to '(void) (request)'

Thanks!:)

dangerman
  • 51
  • 5
  • 3
    To prevent compiler warnings about unused variables – UnholySheep Jul 21 '21 at 10:18
  • `(void)(_p)` has the effect of evaluating the value of `_p`, and converting it to `void`. The conversion has the effect of discarding the value. In your case, it is also used to suppress compiler diagnostics about arguments of a function that are unused. Since evaluation of a raw pointer has no side effects, the net effect of `UNREFERENCED_PARAMETER(request)` is to evaluate the value of `request` and discard that value - and most compilers (if optimisation is enabled) are smart enough to omit the statement entirely. – Peter Jul 21 '21 at 11:48

1 Answers1

2

When you try to compile this code

int main()
{
    int x = 0;
}

with gcc -Werror -Wall you get an error:

<source>: In function 'int main()':
<source>:3:9: error: unused variable 'x' [-Werror=unused-variable]
    3 |     int x = 0;
      |         ^
cc1plus: all warnings being treated as errors
ASM generation compiler returned: 1
<source>: In function 'int main()':
<source>:3:9: error: unused variable 'x' [-Werror=unused-variable]
    3 |     int x = 0;
      |         ^
cc1plus: all warnings being treated as errors

The old-fashioned way to silence that warning (treated as error here) is to write

int main()
{
    int x = 0;
    (void)(x);
}

However, since C++17 there is the attribute [maybe_unused]. Using that gcc does not produce an error with -Wall -Werror for this:

int main()
{
    [[maybe_unused]] int x = 0;
}
Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • Seems like GCC really stresses the "maybe" part. https://godbolt.org/z/qvKe66f1x So shouting about `x` is legit. It's unused period. Probably good to remove it rather than use a macro hack. – StoryTeller - Unslander Monica Jul 21 '21 at 10:40
  • 1
    GCC only produces an error in your answer because of typos. The attribute is `[[maybe_unused]]`, not `[[maybe-unused]]` or `[[maybe_ununsed]]`. – Drew Dormann Jul 21 '21 at 13:45