I have a class with field, which i output to log. If log is turned off (For example in Release), i have warning (private field 'a_' is not used), because i use this field only to output to log.
Sample:
#include <iostream>
//#define TURNON_LOG
#ifdef TURNON_LOG
#define LOG(a) printf("%d", a)
#else
#define LOG(a) 0
#endif
class A
{
public:
A(int a) : a_(a)
{
LOG(a_);
}
private:
int a_;
};
int main(int argc, const char * argv[])
{
A a(10);
return 0;
}
I used clang with -Wall:
clang main.cpp -Wall
What is the best way to fix the warning in case, when TURNON_LOG is undefined?
In addition to Baum mit Augen's answer, in the case of Clang (or any C++17 or later compiler) you can use the [[maybe_unused]]
attribute to silence warnings for specific possibly-unused variables.
class A
{
public:
A(int a) : a_(a)
{
LOG(a_);
}
private:
[[maybe_unused]] int a_;
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With