Boost logo

Boost :

Subject: Re: [boost] Heads up - string_ref landing
From: Dave Abrahams (dave_at_[hidden])
Date: 2012-12-16 15:09:37


on Sun Dec 16 2012, Yakov Galka <ybungalobill-AT-gmail.com> wrote:

> On Sun, Dec 16, 2012 at 12:48 AM, Dave Abrahams <dave_at_[hidden]> wrote:
>
>> on Wed Dec 12 2012, Gottlob Frege <gottlobfrege-AT-gmail.com> wrote:
>> [...]
>> >
>> > Qt's QString has both empty() and isNull() and they are not always the
>> > same. Basically empty() is a zero-byte string, but null is a
>> > never-been-set-or-allocated string.
>>
>> That's dumb, though.
>
> It is the matter of value semantics vs. pointer semantics. QString is "more
> like" a pointer to a referenced counted string.

But that's a dumb thing for QString to be ;-)

>> > ie
>> >
>> > QString name = database.get("Name");
>> >
>> > name.empty() == true means the Name field was empty name.isNull()
>> > == true means the Name field doesn't exist in the database.
>> >
>> > Somewhat like optional<string>.
>> >
>> > I am NOT saying whether this is a good thing. Just a widely known
>> > example of interpretation.
>>
>> I AM saying it's a bad thing! :-)
>>
>
> I agree with you that the null state of QString is a bad thing. But this by
> itself does not justify interpreting an empty() string to be false. Let me
> explain.
>
> The conversion to bool usually tests the validity of the object

Now hold on a minute! I don't buy this premise. In what sense is a
null pointer or a zero integer "invalid?"

> so one can do some other operations on it. For instance this is what
> it means in iostreams (stream is in non-fail state so one can continue
> reading & writing to it)

OK

> and pointers (not null so one can dereference it).

It tests for a useful singular state.

But if you want to follow this line of reasoning...

  front(), back(), and operator[] (probably among others)

can't be used on an empty string.

> These examples are of well understood validness of states. On the
> other hand, for strings, it is dubious that empty() strings are in any
> way invalid.

Just as it's dubious that a null pointer or a zero integer is in any way
invalid.

> All operations still can be done on them, like concatenation, find,
> operator [] with index <= size(), etc...

See above.

> Having said that, personally I do not object to empty() being
> interpreted as false. I am only trying to judge this suggestion
> objectively.

-- 
Dave Abrahams
BoostPro Computing                  Software Development        Training
http://www.boostpro.com             Clang/LLVM/EDG Compilers  C++  Boost

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