2017-03-23 48 views
1

我想在strostrup的std_lib_facilities.h頭文件(這裏是:http://stroustrup.com/Programming/PPP2code/std_lib_facilities.h)在macOS Sierra,Sublime Text 3(通過構建系統),蘋果LLVM版本8.0.0(clang-800.0.38)。我一直在擺弄各種各樣的設置,但一直沒有能夠避免兩個警告,即使開始「Hello,World!」程序編譯和運行:C++ Stroustrup的「std_lib_facilities.h」字符串結構 - 警告:無符號表達式的比較

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++構建設置文件崇高文本從Can't build C++ program using Sublime Text 2複製我的編譯器似乎後沒有認識到Ç ++ 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}'"] 
     } 
    ] 
} 

我會很感激,如果有人可以幫助我瞭解爲什麼這些警告正在發生和解決次EM。

+1

這意味着它是不可能的*無符號*值是負的,並作爲使得所述表達式的一部分總是會被*假*。 – WhozCraig

+0

對,這很有道理 - 我想我一直假設Stroustrup不會寫這樣的冗餘,因此我的編譯器或其他工具的設置有問題。 – wander

回答

2

我會刪除i<0||部分並繼續。

是,C++之父是人類:)

+0

非常感謝,所以絕不是這是我的編譯器,C++版本等設置的問題? – wander

+0

@wander,這是一個組合。你可以調整編譯器設置來忽略這些警告。 –

+0

@wander不,你的編譯器正在產生一個很好的警告。可以關閉該警告,但是警告很好,所以你不想。 – Yakk

相關問題