2017-07-20 120 views
2

我在我的下面幾行代碼:Boost的is_any_of會導致編譯警告?

std::vector<std::string> lines; 
boost::split(lines, output, boost::is_any_of("\n")); 

outputconst std::string

當我編譯,我得到一個錯誤: error: implicit conversion changes signedness: 'typename range_difference<iterator_range<const char *> >::type' (aka 'long') to 'std::size_t' (aka 'unsigned long') [-Werror,-Wsign-conversion] std::size_t Size=::boost::distance(Range); 這源於

boost::split(lines, output, boost::is_any_of("\n")); 
            ^

好像在提升內部打字錯誤? 之前有人遇到過這個嗎?

注意:使用gcc版本5.3.1 20160406(Red Hat 5.3.1-6)進行編譯時,大多數警告標誌設置爲和-Werror。注意2:正如@sehe所示,這確實是一個提升問題。 所以讓我改變這個問題 - 有誰知道這個解決方法嗎?

謝謝!

+0

什麼是'output'?什麼是'線條',真的嗎?因爲你如何分割...字符串集合? (換句話說,創建一個[SSCCE](http://sscce.org/)或[MCVE](https://stackoverflow.com/help/mcve)) – sehe

+0

幾乎不可能在沒有警告的情況下使用boost編譯某些內容由增強膽子引起。它看起來像有人忘了在'std :: size_t Size = :: boost :: distance(Range);'中使用static_cast。 – VTT

+0

@VTT這是一個奇怪的聲明。我猜你正在使用MSVC,在這種情況下,你可以包含一個頭來禁用這些警告。 PS。你重現了錯誤嗎?你有一個SSCCE/MVCE repro? – sehe

回答

0

見此因爲在這個問題上沒有更多的活動,我會說這是 - 這是一個提升問題。它很容易用@sehe的MCVE複製。 顯然編譯提升與嚴格的警告和Werror標誌是不可能的。

按我的解決辦法,我有一個非常簡單的用例,我已經實現了我自己的,簡單的,版本分裂

inline std::vector<std::string> Split(const std::string &str, char delim) 
{ 
    std::vector<std::string> out; 
    std::stringstream ss(str); 
    std::string item; 
    while (std::getline(ss, item, delim)) { 
     out.push_back(item); 
    } 
    return out; 
} 
1

轉載發佈

https://wandbox.org/permlink/LIf0wzUPQxrz7pik(GCC)和https://wandbox.org/permlink/BofswARbx1BpVE6H(鏘)

#include <boost/algorithm/string/split.hpp> 
#include <boost/algorithm/string/predicate.hpp> 
#include <boost/algorithm/string.hpp> 
#include <vector> 
#include <string> 

int main() { 
    std::string output; 
    std::vector<std::string> lines; 
    boost::split(lines, output, boost::is_any_of("\n")); 
} 

這應當報以提高算法的維護者(見boost郵件列表或https://svn.boost.org/trac10/query?status=assigned&status=new&status=reopened&component=string_algo&col=id&col=summary&col=status&col=owner&col=type&col=milestone&order=priority

+0

你沒有添加'-Wsign-conversion' – VTT

+0

@VTT啊。你說得很對。 OP可能已經特別提到了這一點。是的,我同意這是一個值得注意的問題。 – sehe

相關問題