Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Good way to fix warning "field a is not used" if field is unused in configuration

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?

like image 776
Unick Avatar asked Mar 07 '23 04:03

Unick


1 Answers

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_;
};
like image 68
Sneftel Avatar answered Apr 05 '23 22:04

Sneftel