gpt4 book ai didi

C++ Stroustrup 的 "std_lib_facilities.h"字符串结构 – 警告 : comparison of unsigned expression

转载 作者:搜寻专家 更新时间:2023-10-31 02:11:51 24 4
gpt4 key购买 nike

我正在尝试让 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++ build设置文件是从 Can't build C++ program using Sublime Text 2 复制的在我的编译器似乎无法识别 C++11 之后,如 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++之父也是人类:)

关于C++ Stroustrup 的 "std_lib_facilities.h"字符串结构 – 警告 : comparison of unsigned expression,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42968485/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com