Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm interested in creating a macro for eliminating the unused variable warning.

This question describes a way to suppress the unused parameter warning by writing a macro inside the function code:

Universally compiler independent way of implementing an UNUSED macro in C/C++

But I'm interested in a macro that can be used in the function signature:

void callback(int UNUSED(some_useless_stuff)) {}

This is what I dug out using Google (source)

#ifdef UNUSED
#elif defined(__GNUC__)
# define UNUSED(x) UNUSED_ ## x __attribute__((unused))
#elif defined(__LCLINT__)
# define UNUSED(x) /*@unused@*/ x
#elif defined(__cplusplus)
# define UNUSED(x)
#else
# define UNUSED(x) x
#endif

Can this be further expanded for other compilers?

Edit: For those who can't understand how tagging works: I want a solution for both C and C++. That is why this question is tagged both C and C++ and that is why a C++ only solution is not acceptable.

question from:https://stackoverflow.com/questions/7090998/portable-unused-parameter-macro-used-on-function-signature-for-c-and-c

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
319 views
Welcome To Ask or Share your Answers For Others

1 Answer

The way I do it is like this:

#define UNUSED(x) (void)(x)
void foo(const int i) {
    UNUSED(i);
}

I've not had a problem with that in Visual Studio, Intel, gcc and clang.

The other option is to just comment out the parameter:

void foo(const int /*i*/) {
  // When we need to use `i` we can just uncomment it.
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...