对std :: regex_match(和std :: regex_search)的参数的生命周期的约束 [英] Constraints on the lifetime of the argument to std::regex_match (and std::regex_search)

查看:179
本文介绍了对std :: regex_match(和std :: regex_search)的参数的生命周期的约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑C ++ 11函数的signaturer std :: regex_match(
std :: string const& std :: smatch& match,std :: regex const& re)
,什么
是第一个参数的生命周期的约束?我没有找到
任何,但是当我执行以下程序(使用VC ++ 2010编译,
迭代器调试活动):

Considering the C++11 function with the signaturer std::regex_match( std::string const&, std::smatch& match, std::regex const& re ), what are the constraints on the lifetime of the first argument? I don't find any, but when I execute the following program (compiled with VC++ 2010, iterator debugging active):

int
main()
{
    std::string a("aaa");
    std::string c("ccc");
    std::regex re("aaa(.*)ccc");
    std::smatch m;
    if (std::regex_match(a + "xyz" + c, m, re)) {
        std::cout << m[0] << std::endl;
        std::cout << m[1] << std::endl;
    }
    return 0;
}

它崩溃,无疑是因为 sub_match m 只保留
迭代器到字符串,而不是副本。我不能在禁止我的代码
标准中找到任何东西。

it crashes, doubtlessly because the sub_match in m only keep iterators into the string, and not copies. I can't find anything in the standard which forbids my code.

FWIW:它不能在 boost :: regex ,这也是基于
std :: regex 的原因。 (当然,Boost没有记录关于生命周期的任何
约束。)

FWIW: it didn't work in boost::regex, either, and that's what the std::regex is based on. (Of course, Boost didn't document any constraints with regards to the lifetime either.)

最后,我想我的问题是:发送一个DR到
标准组织,或者向Microsoft提交错误报告?

In the end, I guess my question is: should I send in a DR to the standards organization, or a bug report to Microsoft?

推荐答案

在采用 tr1 :: regex std :: regex 期间对此可能性的任何讨论,所以我认为没有考虑。事后看来,这当然是我们应该预见的一个陷阱。在我的头顶部,一个重载,需要一个 std :: string&&& 会指示涉及一个临时,需要一个副本。所以我要向标准委员会报告。 (完全公开:我写了Dinkumware实现,这是微软的船)

I don't recall any discussion of this possibility during the adoption of tr1::regex or std::regex, so I think it simply was not considered. In hindsight, it's certainly a trap that we should have foreseen. Off the top of my head, an overload that takes a std::string&& would signal that a temporary is involved, and a copy is needed. So I'd report it to the Standards Committee. (full disclosure: I wrote the Dinkumware implementation, which is what Microsoft ships)

这篇关于对std :: regex_match(和std :: regex_search)的参数的生命周期的约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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