unused-variable warning different for auto variables

684 views Asked by At

Using gcc (4.7.2 here) I get warnings about unused auto variables, but not about other variables:

// cvars.h
#ifndef CVARS_H_
#define CVARS_H_

const auto const_auto = "const_auto";
const char const_char_array[] = "const_char_array";
const char * const_char_star = "const_char_star";
const char use_me = 'u';

#endif // CVARS_H_

//---
//comp_unit.cpp
#include "cvars.h"

void somef()
{
  //const_auto // commented out - unused
  use_me; // not using any of the others either
}

// compile with $ g++ -std=c++11 -Wunused-variable -c comp_unit.cpp
// gcc outputs warning: ‘cvars::const_auto’ defined but not used [-Wunused-variable]
// but does not complain about the other variables
  1. Is this an inconsistency in GCC?

    1.1 If so, what should happen in all cases, warning or no warning?

    1.2 If not, what is the reason for the difference in behavior?

Note: Concerning 1.1, I imagine no warning should be printed in this case (this is what clang does). Otherwise, any compilation unit including a constant-defining header but not using all the constants within would contain lots of warnings.

1

There are 1 answers

0
Lightness Races in Orbit On BEST ANSWER

These warnings are entirely up to the implementation, so there is no "should". But, yes, I agree: constants would ideally not generate these warnings even when declared using auto.

Since I can reproduce your observation in GCC 4.7 and GCC 4.8.0, but not in GCC 4.8.1 or 4.9, I'd say the guys over at GNU would agree too. In fact, I believe you're seeing bug 57183.