C++ Logo

std-proposals

Advanced search

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

From: Jonathan Wakely <cxx_at_[hidden]>
Date: Thu, 2 Nov 2023 13:47:40 +0000
On Thu, 2 Nov 2023 at 12:53, Chris Gary via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> .... 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.
>
>
> Its concise if used in conjunction with the Elvis operator:
> populateWith( oneVec ?: orAnother )
> printLine( errMsg ?: g_successMsg ) // these could be string_view or string
>

Yes, as I said, the conversion operator is a little less verbose, but at
the possible expense of clarity.

You can easily do that with a helper function that doesn't require a
non-standard extension:
populateWith( chooseNonEmpty(oneVec, orAnother) );

That chooseNonEmpty can be made as generic/complex as you want, but can
also be very simple.

Received on 2023-11-02 13:47:55