简体   繁体   English

regex_token_iterator * it ++ bug?

[英]regex_token_iterator *it++ bug?

for following code: 用于以下代码:

#include<iostream>
#include<regex>

using namespace std;

int main(int argc, char *argv[]) 
{
    regex reg("/");
    string s = "Split/Values/Separated/By/Slashes";
    sregex_token_iterator it{std::begin(s), std::end(s), reg, -1};
    sregex_token_iterator end;

    while(it != end)
    {
        cout << *it++ << endl;
    }

    return 0;
}

should output: 应该输出:

Split
Values
Separated
By
Slashes

but it outputs this: 但它输出这个:

Values
Separated
By

Slashes

the main code may be problem is *it++ , if I write cout << *it << endl;++it; 主要代码可能是问题是*it++ ,如果我写cout << *it << endl;++it; ,it work right. ,它的工作正常。

when I change the stand c++11 regex to boost-regex, *it++ also work right. 当我将stand c ++ 11 regex更改为boost-regex时, *it++也正常工作。

I have check the head of regex, I think the operator++(int) function has no problem. 我检查了正则表达式的头部,我认为operator++(int)函数没有问题。

my clang version is 我的铿锵版是

Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn) 
Target: x86_64-apple-darwin13.0.0
Thread model: posix

Is there anyone have such problem? 有人有这样的问题吗?

Is there a bug in clang? clang中有错误吗?

I found that it's a libc++ implementation bug. 我发现这是一个libc ++实现错误。

Go to regex, insert the following two lines 转到正则表达式,插入以下两行

    regex_token_iterator operator++(int)
    {
        regex_token_iterator __t(*this);
std::cout << "test---" << *__t << "---test" << endl;
        ++(*this);
std::cout << "test---" << *__t << "---test" << endl;
        return __t;
    }

you find that the value of *__t changed after ++(*this) ! 你发现++(* this)之后* __ t的值发生了变化!

Further dig into you will find that, 进一步挖掘你会发现,

*__t is actually implemented by returning internal value_type pointer _ result , while _ result actually points to &_ position ->prefix(), which is the address of match_results' _ prefix object, the address of this object never changed, but the content of it changed. * __ t实际上是通过返回内部value_type指针_ 结果来实现的 ,而_ result实际指向&_ position - > prefix(),这是match_results'_ 前缀对象的地址,这个对象的地址从未改变,但是内容它改变了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM