Boost logo

Boost :

Subject: Re: [boost] [xpressive] Is there a way to test for an empty regex?
From: Eric Niebler (eric_at_[hidden])
Date: 2008-11-20 14:27:05


Andrey Semashev wrote:
> I'm not very familiar with xpressive, but having a look from aside it
> would never come into my mind that "rex.regex_id() == 0" is an emptyness
> (or whatever) check. Sorry, this looks more like a hack to me.
>
> I think a clear statement is needed: is a default-constructed regex
> valid?

Yes!

> If not, then why does it have a default constructor? If it is,
> the default-constructed state should be easily detectable,

Why? Does a std::pair tell you whether it was default-constructed? It's
just a value. How it was constructed doesn't matter.

> since this is
> a common practice in just about any domain:
> - Boost.Regex,

We've already covered why regex::empty is confusing.

> containers, strings,

These are sequences. empty() tests whether the sequence is empty. A
regex is not a sequence. At least not for xpressive.

> Boost.Function provide empty()
> - Boost.Optional, Boost.SmartPtr provide unspecified bool conversions
> and operator!, or equivalent facilities.

These types all have invalid singular values. Regexes do not.

> Personally, I like empty() naming, but if a more precise name can be
> found, I would be fine. And I agree with Michael, such a function should
> be a member of the class.
>
> As for application, the empty state of regex may be useful when the
> expression may, or may not be compiled. It's a sort of implicit
> optional, but a simpler and more efficient one. However, I'm sure it's
> not the only valid use case.

A regex is not like an optional. It does not have an invalid state.

I'm sorry to disappoint you guys. I'm not adding an empty() member function.

-- 
Eric Niebler
BoostPro Computing
http://www.boostpro.com

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk