对于自动变量,unused-variable警告不同

ric*_*cab 2 c++ gcc warnings auto unused-variables

使用gcc(这里是4.7.2)我收到有关未使用的自动变量的警告,但没有关于其他变量的警告:

// 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
Run Code Online (Sandbox Code Playgroud)
  1. 这是海湾合作委员会的不一致吗?

    1.1如果是这样,在所有情况下应该发生什么,警告或没有警告?

    1.2如果不是,行为不同的原因是什么?

注意:关于1.1,我想在这种情况下不应该打印警告(这就是clang所做的).否则,任何包含常量定义标头但不使用其中所有常量的编译单元都会包含大量警告.

Lig*_*ica 5

这些警告完全取决于实施,因此没有"应该".但是,是的,我同意:即使声明使用,常量也不会产生这些警告auto.

由于我可以在GCC 4.7和GCC 4.8.0中重现您的观察结果,但不能在GCC 4.8.1或4.9中重现您的观察结果,我会说GNU的那些人也会同意.事实上,我相信你会看到bug 57183.