C ++ Stroustrup" std_lib_facilities.h"字符串结构 - 警告:无符号表达式的比较

时间:2017-03-23 06:12:27

标签: c++ c++11 build sublimetext3 llvm

我正在努力让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);
    }
};

我的C ++构建设置文件是在我的编译器似乎无法识别C ++ 11之后从Can't build C++ program using Sublime Text 2中复制的,而How do I update my compiler to use C++11 features?中描述了我的C ++构建设置文件:

{
    "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}'"]
        }
    ]
}

如果有人能帮助我理解为什么会发生这些警告并解决它们,我将不胜感激。

1 个答案:

答案 0 :(得分:2)

我会移除i<0||部分并继续。

是的,C ++的父亲也是人类:)