Boost logo

Boost :

From: Rob Stewart (stewart_at_[hidden])
Date: 2005-03-09 16:40:46


From: Matthew Vogt <mattvogt_at_[hidden]>
> Rob Stewart wrote:
> >>I guess it looks okay with c.value(). What do you think?
> >
> > I don't like it.
>
> I don't like it either, but it does have the benefit of simplifying the code by
> removing all the explicit good() tests. I think explicit testing for good
> status is something that's going to be very easy to forget, while you're trying
> to write 'simple' filters that are implemented in get functions which can
> (effectively) be called asynchronously.

Your code had implicit goodness tests. They, too, can be
forgotten, but I understand your point is that this:

   if (c = get(src))

isn't checking whether c is a non-zero character, but whether it
is a good character.

> > That's the right approach: just code in terms of good(c). You
> > get simplified code without the oddity of asking a
> > basic_character for its character. The real value in the
> > suggestion is that one should ignore fail() and eof()
> > conditions in the filter. That's something you can document and
> > your examples can show the simplified form.
> >
>
> But the 'simplified' form will have (c = get(src) && c.good()) everywhere, and
> the abstraction of algorithm state will result in more convoluted boolean
> expressions where testing 'good' will be very easy to omit...

I was thinking of "good(c = get(src))" which isn't convoluted.

> >>You'd be relying on the conversion as a substitute for good, no?
> >
> > Syntactic sugar is nice when it isn't too sweet.
>
> Well, if it helps correctness, it isn't really sugar.

One thing to remember is that a filter is write once, use many
times (modulo bug fixes). Once it works, it works. The
syntactic sugar you're suggesting applies in only a few
expressions in such a filter, so it isn't much of a win, is it?

Let's review. There are several things one needs to do with a
basic_character:

   - return it from a function
   - test it to determine success
   - compare it to a char/wchar_t

Have I missed anything?

Given those requirements the class needs:

   - value semantics
   - safe-bool conversion
   - comparisons with char/wchar_t

Therefore, I think this will work:

template <typename Ch>
class basic_character
{
public:
   basic_character(Ch const);

   Ch value() const;

   operator unspecified-bool-type() const;

   bool operator ==(Ch const) const;
   bool operator !=(Ch const) const;
   bool operator >(Ch const) const;
   bool operator >=(Ch const) const;
   bool operator <(Ch const) const;
   bool operator <=(Ch const) const;

   friend bool good(basic_character const);
   friend bool fail(basic_character const);
   friend bool eof(basic_character const);
   friend bool would_block(basic_character const);

private:
   Ch ch_;
};

Now you can write both of these:

   if (c = get(src))

   if (c == comment_char_)

-- 
Rob Stewart                           stewart_at_[hidden]
Software Engineer                     http://www.sig.com
Susquehanna International Group, LLP  using std::disclaimer;

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