C++ Stroustrup 的 "std_lib_facilities.h" 字符串结构 - 警告:无符号表达式的比较
C++ Stroustrup's "std_lib_facilities.h" String struct – warning: comparison of unsigned expression
我正在尝试让 Stroustrup 的 "std_lib_facilities.h" 头文件(在此处:http://stroustrup.com/Programming/PPP2code/std_lib_facilities.h)在 macOS Sierra、Sublime Text 3(通过构建系统)、Apple LLVM 8.0 版中正确编译。 0 (clang-800.0.38)。我一直在摆弄各种设置,但无法避免两个警告,即使开始 "Hello, World!" 程序正在编译并且 运行:
std_lib_facilities.h:107:8: warning: comparison of unsigned expression < 0 is always false [-Wtautological-compare]
if (i<0||size()<=i) throw Range_error(i);
~^~
std_lib_facilities.h:113:8: warning: comparison of unsigned expression < 0 is always false [-Wtautological-compare]
if (i<0||size()<=i) throw Range_error(i);
~^~
其中有问题的行是以下内容的一部分:
// trivially range-checked string (no iterator checking):
struct String : std::string {
using size_type = std::string::size_type;
// using string::string;
char& operator[](unsigned int i) // rather than return at(i);
{
if (i<0||size()<=i) throw Range_error(i);
return std::string::operator[](i);
}
const char& operator[](unsigned int i) const
{
if (i<0||size()<=i) throw Range_error(i);
return std::string::operator[](i);
}
};
我的 Sublime Text 的 C++ 构建设置文件是从 Can't build C++ program using Sublime Text 2 after my compiler did not seem to recognize C++11 as described in How do I update my compiler to use C++11 features?:
复制的
{
"cmd": ["g++", "-Wall", "-Wextra", "-pedantic", "-std=c++11", "${file}", "-o", "${file_path}/${file_base_name}"],
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "${file_path}",
"selector": "source.c, source.c++",
"variants":
[
{
"name": "Run",
"cmd": ["bash", "-c", "g++ -Wall -Wextra -pedantic -std=c++11 '${file}' -o '${file_path}/${file_base_name}' && '${file_path}/${file_base_name}'"]
}
]
}
如果有人能帮助我理解为什么会出现这些警告并解决它们,我将不胜感激。
我会删除 i<0||
部分并继续。
是的,C++之父也是人:)
我正在尝试让 Stroustrup 的 "std_lib_facilities.h" 头文件(在此处:http://stroustrup.com/Programming/PPP2code/std_lib_facilities.h)在 macOS Sierra、Sublime Text 3(通过构建系统)、Apple LLVM 8.0 版中正确编译。 0 (clang-800.0.38)。我一直在摆弄各种设置,但无法避免两个警告,即使开始 "Hello, World!" 程序正在编译并且 运行:
std_lib_facilities.h:107:8: warning: comparison of unsigned expression < 0 is always false [-Wtautological-compare]
if (i<0||size()<=i) throw Range_error(i);
~^~
std_lib_facilities.h:113:8: warning: comparison of unsigned expression < 0 is always false [-Wtautological-compare]
if (i<0||size()<=i) throw Range_error(i);
~^~
其中有问题的行是以下内容的一部分:
// trivially range-checked string (no iterator checking):
struct String : std::string {
using size_type = std::string::size_type;
// using string::string;
char& operator[](unsigned int i) // rather than return at(i);
{
if (i<0||size()<=i) throw Range_error(i);
return std::string::operator[](i);
}
const char& operator[](unsigned int i) const
{
if (i<0||size()<=i) throw Range_error(i);
return std::string::operator[](i);
}
};
我的 Sublime Text 的 C++ 构建设置文件是从 Can't build C++ program using Sublime Text 2 after my compiler did not seem to recognize C++11 as described in How do I update my compiler to use C++11 features?:
复制的{
"cmd": ["g++", "-Wall", "-Wextra", "-pedantic", "-std=c++11", "${file}", "-o", "${file_path}/${file_base_name}"],
"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
"working_dir": "${file_path}",
"selector": "source.c, source.c++",
"variants":
[
{
"name": "Run",
"cmd": ["bash", "-c", "g++ -Wall -Wextra -pedantic -std=c++11 '${file}' -o '${file_path}/${file_base_name}' && '${file_path}/${file_base_name}'"]
}
]
}
如果有人能帮助我理解为什么会出现这些警告并解决它们,我将不胜感激。
我会删除 i<0||
部分并继续。
是的,C++之父也是人:)