gpt4 book ai didi

c++ - 对 std::regex_match(和 std::regex_search)参数生命周期的约束

转载 作者:太空狗 更新时间:2023-10-29 21:04:10 26 4
gpt4 key购买 nike

考虑带有签名者 std::regex_match(
std::string const&, std::smatch& match, std::regex const& re )
, what对第一个参数的生命周期的限制是什么?我没有找到任何,但是当我执行以下程序时(用 VC++ 2010 编译,迭代器调试事件):

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;
}

它崩溃了,无疑是因为 m 中的 sub_match 只保留迭代器进入字符串,而不是拷贝。我在里面找不到任何东西禁止我的代码的标准。

FWIW:它在 boost::regex 中也不起作用,这就是std::regex 是基于。 (当然,Boost 没有记录任何关于生命周期的限制。)

最后,我想我的问题是:我应该向标准组织,还是向 Microsoft 报告错误?

最佳答案

我不记得在采用 tr1::regexstd::regex 期间讨论过这种可能性,所以我认为根本没有考虑过。事后看来,这当然是一个我们应该预见到的陷阱。在我的脑海中,采用 std::string&& 的重载将表示涉及一个临时文件,并且需要一个拷贝。所以我会向标准委员会报告。 (完全披露:我编写了 Dinkumware 实现,这是 Microsoft 发布的)

关于c++ - 对 std::regex_match(和 std::regex_search)参数生命周期的约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11991964/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com