【问题标题】:unused-variable warning different for auto variables自动变量的未使用变量警告不同
【发布时间】:2014-11-12 18:08:19
【问题描述】:

使用 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
  1. 这是 GCC 中的不一致吗?

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

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

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

【问题讨论】:

    标签: c++ gcc warnings auto unused-variables


    【解决方案1】:

    这些警告完全取决于实施,因此没有“应该”。但是,是的,我同意:即使使用 auto 声明常量,理想情况下也不会产生这些警告。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多