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 18:58:05 +0200
On Thu, 2 Nov 2023 at 18:55, Arthur O'Dwyer via Std-Proposals
<std-proposals_at_[hidden]> wrote:

> Philosophy point: One might object that nobody's proposing to add boolean conversions to every STL type (such as `array` and `bitset` and `path`); the proposal was just to add boolean conversions to some STL containers (like `vector` and `forward_list` and `map`). So we don't need to come up with an overarching rule like "bool(v) must be expression-equivalent to std::ranges::empty(v) for any v under the sun." No such generic rule is even implementable in theory; C++ requires every type to physically provide its own individual member `explicit operator bool() const`, and so the type author must choose the semantics of that `operator bool` (whether it returns .empty() or .any() or .has_value() or .get() or compares to null or whatever). ...But that individualistic state of affairs is exactly the status quo! The author of each STL container (coughStepanovcough) have already made that decision for their containers: that a container is different from a bool. (If anyone should be expected to have a strong opinion as to whether a sequence container is a kind of bool, it's Stepanov! :))

There's plenty of things that are not any kind of bool, but have
contextual convertibility to bool. So this is not about anything being
a kind of bool,
and your suggestions of your being able to channel Stepanov are
misguided, like they are for most people.

Received on 2023-11-02 16:58:19