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, 2 Nov 2023 12:34:09 +0200
On Thu, 2 Nov 2023 at 12:03, Jonathan Wakely via Std-Proposals
<std-proposals_at_[hidden]> wrote:
> Yeah, I don't like this proposal. Just because you *can* collapse some property of containers to a simple true/false, doesn't mean we should add a conversion to actually do that.
>
> Should std::future have a conversion to bool for its valid() member? Or maybe to check if it's ready (an API that's currently missing)?
> Should std:: thread have a conversion to bool for joinable()?
>
> As already pointed out, the Selection statements with initializer feature in C++17 makes it possible to test any arbitrary member function in a condition. That means the advantage of a conversion to bool is just slightly reduced verbosity, but at the cost of clarity (what exactly does it mean for an arbitrary thing to be "true"?)

It means "can I dereference this thing or walk its range". When that's
uniform, you can write generic code for it.
"Just use .empty()", well, pointers don't have it, optional doesn't have it.

No, we don't need to add that to everything, but adding it to
std::future wouldn't be far-fetched. Adding it to std::thread is not
so obvious,
because you don't dereference that or walk its range, but a generic
validity check has its own value - you could write a generic
checking-guard.

The whole if ("") vs. if (""sv) is a complete red herring - those
don't appear in any generic context, and in fact those don't appear
anywhere.
Nobody writes that. Nobody is confused by it.

Received on 2023-11-02 10:34:24