【发布时间】:2016-03-19 05:44:11
【问题描述】:
我安装了 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 的示例错误?
【问题讨论】:
-
cppreference.com 示例严格遵守标准,错误发生在 C 运行时而不是抛出异常,所以我确定这是 STL 实现中的错误。至少 MSVC 开发人员非常聪明地为这种预期情况添加了断言。
-
我建议使用 boost 正则表达式,STL 正则表达式的实现通常是错误的(至少对我而言),切换到 boost 只需要更改命名空间。
标签: c++ visual-c++