Talk:cpp/regex/match results/empty
May I suggest to add a note whether it is legal or UB to access empty() (or size()) if the reglar expression to which the std::match_results object was an argument didn't match?
I found this as a minor difference between the C++ standard library implementation on Linux and Boost.Regex. The same code linked with the regular expression implementation in the Linux C++ standard has no problem with that access but the boost implementation throws
what(): Attempt to access an uninitialzed boost::match_results<> class.
I'm aware this is a bit Linux/Boost specific and a border-case topic for a C++ site, but it makes sense in so far as the Linux C++ standard library had a broken implementation of regular expressions for a long time. Therefore Linux developers with not quite recent versions will still have to use Boost.Regex as a drop-in replacement - usually not a big problem switching via namespace alias, except such small nuisances.
And of course, it might still be the case that accessing an std::match_results object after an unsuccessful attempt to match is UB and my program worked just out of luck, i.e. Boost.Regex was right to be a bit more picky.
Mwe (talk) 07:50, 7 March 2016 (PST)
- It's legal. empty() is equivalent to size() == 0, and size() is required to return "One plus the number of marked sub-expressions in the regular expression that was matched if *this represents the result of a successful match. Otherwise returns 0." That is, if *this does not represent the result of a successful match, size() shall return 0 and empty() shall return true. --D41D8CD98F (talk) 08:59, 7 March 2016 (PST)