C++ Logo

std-proposals

Advanced search

Re: [std-proposals] ^^operator [was: Re: New draft proposal: Add "%s" (two-digit truncated-integer seconds) as a std::format conversion specifier for std::chrono time types.]

From: Lénárd Szolnoki <cpp_at_[hidden]>
Date: Thu, 11 May 2023 16:49:20 +0100
Hi,

On 11 May 2023 15:26:38 BST, Sebastian Wittmeier via Std-Proposals <std-proposals_at_[hidden]> wrote:
>Hi Magnus,
>
>when there was talk here about a ^^ operator for logical eXclusive OR, I was looking for precedence in other languages.
>
>
>There are two differences in Raku regarding shortcut operators and ^ to the usual C like notations:
>
>
>1. Shortcut Operators return one of the arguments
>
>Raku's shortcut logical operators tests for True- or False-ness of their arguments, but return one of the original arguments.
>
>However, Raku's non-shortcut logical operators tests for True- or False-ness of their arguments and return a boolean True or False.
>
>
>2. Both ^ and ^^ test for exactly one True value instead of for an odd number of True values (parity)
>
>This makes ^^ a true short-cut operator, as it stops (and ^ returns False and ^^ returns Nil), after/if a second true value has been encountered
>
>
>I would not propose to change the existing well-established operators in C++.
>
>
>But this is an interesting functionality:
>
> - && returns the first False argument (otherwise the last argument)
>
> - || returns the first True argument (otherwise the last argument)

There is an operator with this behaviour supported by some C++ compilers as an extension: the "Elvis" operator , spelled as "?:".

> - ^^ returns the only True argument (or Nil if more than one otherwise the last argument)
>
> - // returns the first defined argument (otherwise the last argument)
>
> - min returns the smallest argument
>
> - max returns the largest argument
>
>
>In C++ speak the original argument is used and recovered after an implicit conversion.
>
>In C++ this can be compared with std::ranges::find_if, std::ranges::find_if_not and std::ranges::minmax with the use of a projection to bool or int.
>
>
>Best,
>
>Sebastian
>
>
>
>-----Ursprüngliche Nachricht-----
>Von:Magnus Fromreide <magfr_at_[hidden]>
>Gesendet:Do 11.05.2023 08:21
>Betreff:Re: [std-proposals] ^^operator [was: Re: New draft proposal: Add "%s" (two-digit truncated-integer seconds) as a std::format conversion specifier for std::chrono time types.]
>An:Sebastian Wittmeier via Std-Proposals <std-proposals_at_[hidden]>;
>CC:Jonathan Wakely <cxx_at_[hidden]>; Sebastian Wittmeier <wittmeier_at_[hidden]>;
>On Thu, May 04, 2023 at 10:54:21AM +0200, Sebastian Wittmeier via Std-Proposals wrote:
>> Should be:
>>
>>
>> auto operator^^(auto A, auto B)
>>
>> {
>>
>> if(A && !B)
>>
>> return A;
>>
>> if(B && !A)
>>
>> return B;
>>
>> return nil;
>>
>> }
>>
>
>That is a surprising formulation - not bad but surprising.
>
>Consider operator ||, do you propose to change it to something along the
>lines of
>
>auto operator||(auto A, auto B)
>{
>if (A)
>return A;
>else
>return B;
>}
>
>but with extra magic to short-circut?
>
>/MF
>
>

Received on 2023-05-11 15:49:29