![](/img/trans.png)
[英]Why do gcc and clang give different results in aggregate initialization?
[英]Why do different GCC 4.9.2 installations give different results for this regex match?
#include <iostream>
#include <regex>
#include <string>
int main()
{
std::string example{" <match1> <match2> <match3>"};
std::regex re{"<([^>]+)>"};
std::regex_token_iterator<std::string::iterator> it{example.begin(), example.end(), re, 1};
decltype(it) end{};
while (it != end) std::cout << *it++ << std::endl;
return 0;
}
兩個站點都使用GCC 4.9.2。 我不知道ideone使用什么編譯參數,但Coliru中沒有什么不尋常之處。
Coliru沒有給我match1
結果:
# g++ -v 2>&1 | grep version; \
# g++ -std=c++14 -O2 -Wall -pedantic -pthread main.cpp && ./a.out
gcc version 4.9.2 (GCC)
match2
match3
match1
match2
match3
我的代碼是否有未定義的行為或什么? 什么可能導致這個?
這是libstdc ++的regex_token_iterator
拷貝構造函數中的一個錯誤,由postincrement運算符調用。 該錯誤已於2014年12月修復; 從那時起發布的gcc 4.9和5.x版本將有修復。 錯誤的本質是迭代器的副本將副本的目標別名化,從而導致觀察到的行為。
解決方法是使用preincrement - 從微觀優化的角度來看這也是可取的,因為regex_token_iterator
是一個相當重的類:
for (; it != end; ++it) std::cout << *it << std::endl;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.