0

In the function below I'm not using the parameter (void **arg). But since it's unused inside the function compiler gives me the error below:

error: unused parameter 'arg' [-Werror=unused-parameter]
bool decodeString(pb_istream_t *stream, const pb_field_t *field, void **arg)

I tried to suppress it by writing void(arg) inside the function without any luck. Can anyone help me with the correct way?

razz
  • 47
  • 5
  • 4
    Leave out the variable name: `bool decodeString(pb_istream_t *stream, const pb_field_t *field, void **arg)` becomes `bool decodeString(pb_istream_t *stream, const pb_field_t *field, void **)` – user4581301 Oct 27 '21 at 18:15
  • 1
    Side note: I prefer to comment out the name `bool decodeString(pb_istream_t *stream, const pb_field_t *field, void ** /*arg*/ )` because the name may hold useful information about how the parameter should be used, should that information become important later. – user4581301 Oct 27 '21 at 18:16
  • 1
    Does this answer your question? [How to suppress "unused parameter" warnings in C?](https://stackoverflow.com/questions/3599160/how-to-suppress-unused-parameter-warnings-in-c) – lulle2007200 Oct 27 '21 at 18:17
  • @MadPhysicist only viable in C++. dbush's answer below works in C and C++. Mind you, my C is pretty stale. Might have changed. – user4581301 Oct 27 '21 at 18:20
  • @user4581301 - I 2nd the 2nd comment. – ryyker Oct 27 '21 at 18:21
  • See [my answer to *Empty function macros*](https://stackoverflow.com/a/9187855/5987). – Mark Ransom Oct 27 '21 at 18:21

1 Answers1

4

Use the parameter in an expression casted to void. Then the parameter is "used".

bool decodeString(pb_istream_t *stream, const pb_field_t *field, void **arg)
{
    (void)arg;
    ...
}
dbush
  • 205,898
  • 23
  • 218
  • 273
  • This does not work, found similar answer in https://stackoverflow.com/questions/3599160/how-to-suppress-unused-parameter-warnings-in-c and tried it before. error: variable or field 'arg' declared void void(arg); – razz Oct 27 '21 at 18:21
  • Be *very* careful with doing this as conceptually you have induced a read of the value. Personally I'd comment `arg` - you don't lose the self-commenting nature of the function and you can always uncomment it if you need it. This is a good idiomatic (especially in C) technique though. Although I do remember reading something about a nasty bug emanating from this, with gcc and optimisations turned up to the maximum. Might have dreamt it though. – Bathsheba Oct 27 '21 at 18:22
  • 2
    @razz, if this doesn't work in C you have a second problem. – user4581301 Oct 27 '21 at 18:22
  • you can also `int decodeString(void **) { /* ... */ }` – 0___________ Oct 27 '21 at 18:25
  • @razz Then you typed something in wrong. `(void)arg;` in the function body compiles without warning. – dbush Oct 27 '21 at 18:25
  • I see `void(arg)` called out as a failure in the question. You might have just bracketed the wrong term. – user4581301 Oct 27 '21 at 18:26
  • @user4581301 you're right! (void)arg works, my second problem is: libboost_thread.so.1.68.0: error adding symbols: DSO missing from command line – razz Oct 27 '21 at 18:48
  • 1
    @razz That's a separate issue that should be in a separate question. – dbush Oct 27 '21 at 18:49