gcc 4.8 或更早版本是否有关于正则表达式的错误? [英] Is gcc 4.8 or earlier buggy about regular expressions?

查看:43
本文介绍了gcc 4.8 或更早版本是否有关于正则表达式的错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 C++11 代码中使用 std::regex,但似乎支持有点问题.一个例子:

#include #include int main (int argc, const char * argv[]) {std::regex r("st|mt|tr");std::cerr <<st|mt|tr"<<"匹配 st? " <<std::regex_match("st", r) <<std::endl;std::cerr <<st|mt|tr"<<"匹配山?" <<std::regex_match("mt", r) <<std::endl;std::cerr <<st|mt|tr"<<" 匹配 tr? " <<std::regex_match("tr", r) <<std::endl;}

输出:

st|mt|tr 匹配 st?1st|mt|tr 匹配 mt?1st|mt|tr 匹配 tr?0

当用 gcc (MacPorts gcc47 4.7.1_2) 4.7.1 编译时,要么用

g++ *.cc -o test -std=c++11g++ *.cc -o test -std=c++0x

g++ *.cc -o test -std=gnu++0x

此外,如果我只有两种替代模式,则正则表达式效果很好,例如st|mt,所以看起来最后一个由于某些原因没有匹配.该代码适用于 Apple LLVM 编译器.

关于如何解决问题的任何想法?

更新 一种可能的解决方案是使用组来实现多种替代方案,例如(st|mt)|tr.

解决方案

在 GCC 4.9.0 中实现并发布.

在您(旧)版本的 GCC 中,未实施.

当 GCC 的所有 C++0x 支持高度实验性、跟踪早期 C++0x 草案并可供使用时,添加了该原型 代码供人们试验.这使人们可以在标准最终确定之前发现问题并向标准委员会提供反馈.当时,很多人都庆幸早在 C++11 完成之前以及在许多其他编译器提供任何支持之前就能够访问最前沿的特性,而这种反馈确实有助于改进 C++11.这是一件好事TM.

<regex> 代码从未处于有用状态,而是像当时的许多其他代码一样作为正在进行的工作添加.它被签入并提供给其他人,如果他们愿意,可以进行协作,目的是最终完成.

这通常是开源的工作方式:尽早发布,经常发布——不幸的是在在 <regex> 的情况下,我们只得到了早期的部分,而不是完成实现的经常部分.

库的大部分内容更加完整,现在几乎完全实现,但 没有实现,所以它自添加以来一直处于未完成状态.><块引用>

说真的,谁认为发布只执行返回假"的 regex_search 实现是个好主意?

几年前这并不是一个坏主意,当时 C++0x 仍在开发中,我们发布了许多部分实现.没有人认为它会长期无法使用,所以事后看来,也许它应该被禁用并需要一个宏或内置时间选项来启用它.但那艘船很久以前就航行了.libstdc++.so 库中导出的符号依赖于正则表达式代码,因此简单地将其删除(例如,在 GCC 4.8 中)并非易事.

I am trying to use std::regex in a C++11 piece of code, but it appears that the support is a bit buggy. An example:

#include <regex>
#include <iostream>

int main (int argc, const char * argv[]) {
    std::regex r("st|mt|tr");
    std::cerr << "st|mt|tr" << " matches st? " << std::regex_match("st", r) << std::endl;
    std::cerr << "st|mt|tr" << " matches mt? " << std::regex_match("mt", r) << std::endl;
    std::cerr << "st|mt|tr" << " matches tr? " << std::regex_match("tr", r) << std::endl;
}

outputs:

st|mt|tr matches st? 1
st|mt|tr matches mt? 1
st|mt|tr matches tr? 0

when compiled with gcc (MacPorts gcc47 4.7.1_2) 4.7.1, either with

g++ *.cc -o test -std=c++11
g++ *.cc -o test -std=c++0x

or

g++ *.cc -o test -std=gnu++0x

Besides, the regex works well if I only have two alternative patterns, e.g. st|mt, so it looks like the last one is not matched for some reasons. The code works well with the Apple LLVM compiler.

Any ideas about how to solve the issue?

Update one possible solution is to use groups to implement multiple alternatives, e.g. (st|mt)|tr.

解决方案

<regex> was implemented and released in GCC 4.9.0.

In your (older) version of GCC, it is not implemented.

That prototype <regex> code was added when all of GCC's C++0x support was highly experimental, tracking early C++0x drafts and being made available for people to experiment with. That allowed people to find problems and give feedback to the standard committee before the standard was finalised. At the time lots of people were grateful to have had access to bleeding edge features long before C++11 was finished and before many other compilers provided any support, and that feedback really helped improve C++11. This was a Good ThingTM.

The <regex> code was never in a useful state, but was added as a work-in-progress like many other bits of code at the time. It was checked in and made available for others to collaborate on if they wanted to, with the intention that it would be finished eventually.

That's often how open source works: Release early, release often -- unfortunately in the case of <regex> we only got the early part right and not the often part that would have finished the implementation.

Most parts of the library were more complete and are now almost fully implemented, but <regex> hadn't been, so it stayed in the same unfinished state since it was added.

Seriously though, who though that shipping an implementation of regex_search that only does "return false" was a good idea?

It wasn't such a bad idea a few years ago, when C++0x was still a work in progress and we shipped lots of partial implementations. No-one thought it would remain unusable for so long so, with hindsight, maybe it should have been disabled and required a macro or built-time option to enable it. But that ship sailed long ago. There are exported symbols from the libstdc++.so library that depend on the regex code, so simply removing it (in, say, GCC 4.8) would not have been trivial.

这篇关于gcc 4.8 或更早版本是否有关于正则表达式的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆