Visual Studio 2015 Update 2 RC中的regex_token_iterator问题

时间:2016-03-17 10:34:08

标签: c++ visual-c++

我安装了Visual Studio 2015 Update 2 Release Candidate,现在看起来使用sregex_token_iterator时遇到了问题,到目前为止似乎工作正常。要验证我是否尝试了cppreference.com中的以下示例代码(请注意,我已将变量text更改为在末尾有空格):

#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实现中的错误还是regex_token_iterator错误的示例?

1 个答案:

答案 0 :(得分:1)

很抱歉 - 作为我们在更新2的<regex>中执行的性能修复的一部分,我们不再制作大量临时字符串对象;如果sub_match实例与某些东西不匹配,我们只需创建值初始化的迭代器,其行为就像&#34;好像&#34;有一个空字符串匹配。

这个程序从C ++ 14开始应该是有效的(在regex_token_iterator内部发生的事情是道德的);见&#34; null forward iterators&#34;:

#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碰巧碰到它。

请注意,在发布版本中(关闭调试断言),这一切都可以正常工作;错误是在迭代器调试机器中,而不是在迭代器&#39;行为。

此错误将在2015 Update 2 RTM中修复。