"ALWAYS use a named regex, NEVER a temporary" - why?
Stephan T. Lavavej in his regex presentation mentions that one should never use temporary regex object. I could think of some efficiency reasons of course. But it looks like there are more "explicit" reasons to avoid temporary objects. A sample code stops working once I replace a named regex with a temporary one. Do you know what is the exact explanation for that?
UPD: the sample code 开发者_如何学编程from Stephan's presentation is broken if using a temporary object:
const regex r("\\w*day");
string s;
getline(cin, s);
const sregex_iterator end;
// works:
//for (sregex_iterator i(s.begin(), s.end(), r);
// doesn't work:
for (sregex_iterator i(s.begin(), s.end(), regex("\\w*day"));
i != end; ++i)
{
cout << (*i)[0] << endl;
}
The advantage of a named regex (with boost at least) is that it may be compiled only once. This is a performance advantage that can make a big difference if the regular expression is applied many times.
I'd never put a guideline like that in wordings like 'NEVER do this', by the way. The truth is, that
Once you store the regex, it is not impossible to accidentally recompile it every time anyway
(if you store it but forget the the static const
and flags to to make the regex keep its compiled state machine etc.)
Also, some regex libraries will even do an automatic caching of the compiled regexen, that uses a statiscal (MRU) method to optimize it. That may in some circumstances be even better than manually keeping a 'named' a regex.
Thinking more about the issue I have realized that constructor of regex_iterator
takes a reference to regex
, which of course explains the problem. Passing a reference to a temporary object is obviously a mistake.
精彩评论