2016-03-17 79 views
1

我安裝了Visual Studio 2015 Update 2 Release Candidate,現在似乎遇到了一個使用sregex_token_iterator的問題,目前爲止似乎工作正常。爲了驗證我試圖從cppreference.com下面的示例代碼(請注意,我改變了可變text有在最後一個空格):Visual Studio 2015 Update 2中的regex_token_iterator問題RC

#include <iostream> 
#include <regex> 

int main() { 
    std::string text = "Quick brown fox "; 
    // tokenization (non-matched fragments) 
    // Note that regex is matched only two times: when the third value is obtained 
    // the iterator is a suffix iterator. 
    std::regex ws_re("\\s+"); // whitespace 
    std::copy(std::sregex_token_iterator(text.begin(), text.end(), ws_re, -1), 
       std::sregex_token_iterator(), 
       std::ostream_iterator<std::string>(std::cout, "\n")); 
} 

運行此給出以下論斷:

Debug Assertion Failed! 

Program: C:\WINDOWS\SYSTEM32\MSVCP140D.dll 
File: c:\program files (x86)\microsoft visual studio 14.0\vc\include\xstring 
Line: 247 

Expression: string iterators incompatible 

For information on how your program can cause an assertion 
failure, see the Visual C++ documentation on asserts. 

是那Visual Studio STL實現中的一個bug或者是regex_token_iterator的例子錯誤?

+0

cppreference.com示例嚴格遵守標準,錯誤發生在C運行時而不是拋出異常,所以我確信它是STL實現中的一個錯誤。至少MSVC開發者非常聰明,可以爲這個預期的情況添加一個斷言。 – Youka

+0

我建議使用boost正則表達式,STL正則表達式實現通常是buggy(至少對我來說),而切換到boost只需要改變命名空間。 – Youka

回答

1

對不起 - 作爲性能修復的一部分,我們在更新2的<regex>中做了一些修改,我們不再生產大量的臨時字符串對象;如果一個sub_match實例不匹配某個東西,我們只做一個值初始化的迭代器,它的行爲「好像」存在空字符串匹配。

該程序應該從C++ 14開始有效(在道德上regex_token_iterator裏面發生了什麼);請參閱「null forward iterators」:

#include <stdio.h> 
#include <string> 

int main() { 
    // Value constructed iterators conceptually point to an empty container 
    std::string::iterator beginIt{}; 
    std::string::iterator endIt{}; 
    printf("This should be zero: %zu\n", endIt - beginIt); 
} 

...但我們的調試聲明禁止這樣做。 regex_token_iterator只是碰巧絆倒它。

請注意,在發佈版本中(關閉調試斷言),這一切都會正常工作;該錯誤在迭代器調試機器中,而不在迭代器的行爲中。

此錯誤將在2015 Update 2 RTM中修復。

相關問題