Considering the C++11 function with the signaturer std::regex_match(, what
std::string const&, std::smatch& match, std::regex const& re )
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;
}
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: 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.)
In the end, I guess my question is: should I send in a DR to the
standards organization, or a bug report to Microsoft?
I don’t recall any discussion of this possibility during the adoption of
tr1::regexorstd::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 astd::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)