为什么GCC中的-Wunused-variable即使在静态const上也会产生错误?
Why does -Wunused-variable in GCC produce an error even on static const?
我有一个标题,core/types.hh
,由几个不同的构建目标使用。它有以下声明:
core/types.hh
typedef std::size_t Size;
static const Size SZ_MAX = std::numeric_limits<Size>::max();
...
有些目标使用这个常数,有些不使用。所以我得到:
error: 'core::SZ_MAX' defined but not used"
我在Linux上使用GCC 4.7.3的scons。我设置了-Wall
,并希望保持这种状态。
根据我对GCC文档的理解,这应该不会给出警告:
-Wunused-variable
当局部变量或非常量静态变量在声明之外未被使用时发出警告。此警告由
启用-Wall.
所以我不明白为什么我得到一个警告(它变成了一个错误)。
在其他答案中,人们被建议声明extern
,并在使用该常数的文件中进行赋值。该文件被许多其他文件使用,因此如果我这样做,它将失去其恒定性。此外,这个文件有头保护,所以我认为这应该意味着这个常量实际上只创建一次。
我很感激任何帮助!
、
可能的重复:
- 如何使用"未使用的变量"警告类型常量? 在h文件中声明的c++静态数组给出警告'已定义但未使用'
似乎这是不是停止编译的错误。
相反,如果GCC发现另一个错误,它仍然会报告这个。
我实际上有另一个未使用的变量,这就是导致这个错误的原因。
例如,创建以下文件:
file1.cc
#include "head1.hh"
int main() {
int bad_unused_variable;
return my_ns::JUST_ANOTHER_CONST;
}
head1.hh
#ifndef HEAD1
#define HEAD1
#include <stdint.h>
#include <cstddef>
#include <limits>
namespace my_ns {
typedef std::size_t Size;
static const Size SZ_MAX = std::numeric_limits<Size>::max();
static const Size JUST_ANOTHER_CONST = 8;
}
#endif
你
:
> g++ -Wall -Werror file1.cc -O2 -std=c++98 -o file1
file1.cc: In function 'int main()':
file1.cc:4:6: error: unused variable 'bad_unused_variable' [-Werror=unused-variable]
In file included from file1.cc:1:0:
head1.hh: At global scope:
head1.hh:10:20: error: 'my_ns::SZ_MAX' defined but not used [-Werror=unused-variable]
cc1plus: all warnings being treated as errors
编辑这似乎也已经在这里得到了回答:gcc警告:定义但未使用的变量与未使用的变量-在那里他们提到了两个警告消息(unused variable
vs defined but not used
)之间的细微差异。尽管如此,它并没有真正回答为什么 GCC会这样做…
相关文章:
- 在运算符重载定义中使用成员函数(const错误)
- 在函数中使用 const int size 参数创建数组会在 Visual Studio 中抛出错误 C++:表达式的计
- 需要帮助在 c++ 中将字符串转换为字符 ----错误 "const char *" 类型的值不能用于初始化 "char" 类型的实体
- 类型为 "Bucket&"(未限定的 const 限定)的引用不能使用 "SortedList." 类型的值进行初始化 如何修复此错误?
- 在gcc中意外调用了Const重载.编译器错误或兼容性修复程序
- 在其他容器中使用 boost::container::static_vector 时,GCC 编译错误"将'const s'绑定到类型's&'的引用丢弃限定符"
- 为什么我的编译器在调用 const getter 函数时抛出"转换丢失(const)限定符"错误?
- C++ 错误:由于 swprintf 而从 'int' 到 'const wchar_t* 的转换无效
- test1.cpp:9:77:错误:对"(const std::normal_distribution) <double>(std::mt19937&)"的调用不匹配
- 错误:对类型 'const ItemInstance' 的引用无法绑定到类型 'void' 的右值
- 错误:从'void*'到'const uint8_t* {aka const unsigned char*}'的转换无效 [-允许]
- C++ Boost - 序列化错误 - 将"const B"作为"this"参数
- 错误:从 'char' 到 'const char' [-允许] strcat(加密,密钥[i])的转换无效;
- 索引 2D 向量时'Reference to type const..'编译错误
- 编译器在传递 const 变量时返回错误:模板参数不是常量表达式
- 'note: 候選人: Volume& Volume::operator=(const Volume&)'.这个错误是什么?
- 错误:二进制">>":未找到采用类型为"const char [1] 的右操作数的运算符
- 类型"const char[2]"的值不能隐式转换为"int"错误C++
- 错误:“ const char*”类型的操作数和“ const char [7]”类型为二进制“操作员 ”
- 错误:' const '之前预期的未限定id