C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Bool casting operator to check if a container not empty

From: Ville Voutilainen <ville.voutilainen_at_[hidden]>
Date: Thu, 19 Oct 2023 19:53:42 +0300
On Thu, 19 Oct 2023 at 19:46, Giuseppe D'Angelo via Std-Proposals
<std-proposals_at_[hidden]> wrote:
>
> On 19/10/2023 18:28, Sebastian Wittmeier via Std-Proposals wrote:
> > The question is, whether this is too confusing or typical (or should
> > be) for std library classes to show their size as implicit bool or
> > implicit int.
>
> Let's open Pandora's box, shall we?
>
> std::string s = "false";
> if (s) { ... }
>
> Is `s` expected to be true or false here?

It's perfectly obvious that s is not empty, so it's true.

> Is std::string("123") == 123
> true or false?

Ill-formed, because the bool conversion operator for a container would
be explicit,
and code like that can't call it.

> These operations don't make sense and should not be added.

I daresay I disagree with that. They make perfect sense and would
bring more uniformity into the language.

The major reason why this thing was frowned upon in 2009 when I filed
an NB comment suggesting
such a change is that

if ("")

is true. I think it's wrong to reject the idea because of that,
because that's just a weird case, but there's nothing
surprising about

if (s)

in your example.

Received on 2023-10-19 16:53:56