C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Unary operator | (absolute value of)

From: Thiago Macieira <thiago_at_[hidden]>
Date: Thu, 23 Nov 2023 19:30:19 -0800
On Thursday, 23 November 2023 18:03:30 PST Jason McKesson via Std-Proposals
wrote:
> > Operator <=> is the only operator we've added since C++ was first
> > standardised. It was added because the three-way comparison needed to be
> > a language feature so we could have the other relational operators
> > synthesised from it, otherwise we could have simply done with a library
> > solution with extension points.
> To be completely fair, there are two other operators. The user-defined
> literal operator and `operator co_await`. But your point is otherwise
> valid, as at least some parts of these are things that have to be part
> of the language.

The UDL operator is a syntax gimmick to allow a language extension. It's not
really an operator because you can't call it on a variable, only on literals.
And its syntax in a single token in the lexer - you can't surround it by
spaces like any other operator.

The wordy operators are definitely more extensible and I don't think the
standard would have a problem adding more of them, if there's reason, but also
minding that those become reserved keywords. C had only one of them (sizeof),
and the original C++ introduced two more (new and delete). We added alignof
too.

Aside from the spaceship, the only other symbolic operators C++ has ever added
on top of C are the PMF-calling ones, .* and ->*.

In the proposal, it's probably a good idea to also discuss the potential
parsing pitfalls. For example:

  a | + b;

This is currently operator|(a, +b) and I suppose it won't change.

It would also allow for accidentally breaking a logical-OR:

  a || b
  a | | b


The one reason I'd like to see this is to have fun finding secret operators:
 Operator Tee: -| a
 Operator Corkscrew 1: |~~ a
 Operator Corkscrew 2: |-~ a or ~-| a (builds upon inchworm-on-a-stick)
 Operator Icepick: |-- a
 Operator JC: -|-- a
 x |=| y


And if trigraphs are allowed...
 x and??! y
 
-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
   Software Architect - Intel DCAI Cloud Engineering

Received on 2023-11-24 03:30:22