2016-03-17 39 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中修复。

相关问题