-1

I have a macro in my C++ code, macro has an unused variable. I am getting warning for that variable the macro is to print the class and method name

#define LOG_ENTER(func_name, message)                                      \
    LOG_SET_METHOD(#func_name)                                             \
    LOG_MOD_INTERNAL(TC_TAG(ENTER) << message)
#define LOG_SET_METHOD(name) static const char LOG__METHOD__[] = "::" name "() ";

We are using gcc version 4.4.6 20110731 (Red Hat 4.4.6-3) (GCC).

"warning: unused variable "LOG__METHOD__" "

How to suppress this warning? It causing more noise!!

rkk
  • 51
  • 1
  • 4
  • 1
    Show the macro and how you're using it *in your question*. And include the toolchain info. Warning disablement is frequently fickle to the tools being used. – WhozCraig Apr 15 '15 at 07:31
  • 3rd answer at: http://stackoverflow.com/questions/15053776/how-do-you-disable-the-unused-variable-warnings-coming-out-of-gcc may be helpful – CygnusX1 Apr 15 '15 at 07:51
  • What is the purpose of the unused variable? – Otomo Apr 15 '15 at 08:03

2 Answers2

0

The usual way of silencing this warning is to use the variable in a dummy expression :

int main() {
    int i;
    i;
}

However, this triggers "Warning : statement has no effect", because i has no side effect and its value is not used. To silence this one, we explicitly ignore the value :

int main() {
    int i;
    (void)i;
}

And there goes the warning.

Quentin
  • 62,093
  • 7
  • 131
  • 191
  • This is a hack (IMHO) and compiler attributes would be a much clearer and cleaner solution (gcc's `__attribute__((unused))`). – Otomo Apr 15 '15 at 08:02
  • @Otomo well it's [the standard way to do it](http://stackoverflow.com/a/7160837/3233393). And the syntax can't really be mistaken for anything else. No need to resort to compiler magic. – Quentin Apr 15 '15 at 08:55
  • It is sometimes nice to see that C++ settles such a low threshold about what can be a statement. – cppBeginner Dec 15 '21 at 04:35
0

A way to disable warning:

template <typename T>
void UnusedVar(const T&) {}

And then use:

UnusedVar(my_var);

Casting to void is also a common way (but doesn't work for all compiler):

(void) my_var; // or static_cast<void>(my_var)
Jarod42
  • 203,559
  • 14
  • 181
  • 302