Committee: ISO/IEC JTC1 SC22 WG21 EWG Evolution
Document Number: P0611R0
Authors: Lawrence Crowl
Reply To: Lawrence Crowl, Lawrence@Crowl.org
Audience: EWG Evolution
C++ can and should extend and improve its set of operators. Attention to several approaches and principles can reduce negative impacts from doing so. This paper provides two examples of improving operator support.
Examples Of Change
A Better Dereference Operator
A Better Exclusive Or Operator
A New Exponentiation Operator
Table of Operators
When introduced, C was notable for its rich set of operators. Many subsequent languages have adopted those operators. They have also extended those operators, so that today the C operator set appears rather small. C++ is one of those subsequent languages, but one that has been more conservative in adding operators. See the Table of Operators for the current C and C++ operators.
The following table gives a sampling of extended operators in several languages. Named operators are particularly problematic, as sometimes they require more special syntax than operators, and sometimes they are not listed with the punctuation operators. I have omitted all compound assignment operators. Entries may not be correct or complete.
How do we extend the operator set in a way that meets the need for concise syntax while not producing unparsable or unreadable expressions?
In addition, how do we remove operators that are problems?
There are several approaches to adding operators.
Existing examples include
There exist proposals suggesting
One problem with this approach is that
what was formerly two tokens can become one token.
a**b changes meaning
if one adds a Fortran-style
** exponentiation operator.
A careful examination of possible adjacent operators will reduce the likelyhood of problematic operator introduction. In general, one should avoid concatenation ambiguity between postfix operators and infix operators as well as between infix operators and prefix operators.
Some rare ambiguities may be acceptable.
For example, the code
changed meaning with the introduction of
This change was not a significant problem in practice.
Automated tools could help.
One problem with this approach is that
identifers in use in existing programs can suddenly become ill-formed.
I had one customer strenuously objecting
to introduction of the
invalidating the public interface of his library.
The policy of vetting keywords by searching open source software has reduced the severity of this problem.
The most widely known example of this kind of extension
is that of using the infix subtraction operator
as prefix negation operator.
An example more specific to C and C++
is the extension of the infix operator
to the prefix operator
While all such 'fixity' extensions will yield parsable expressions, such expression may not be easily written or read. To avoid confusion, it is probably best that any given operator have at most two of the three fixities.
A more subtle problem is adding a fixity
that would naturally bring two tokens together
that would be interpreted as a different combined token.
The best example of this problem
is the introduction of the
as a postfix 'close template argument list' operator.
Closing two lists simultaneously led to the right shift operator.
Some programming languages use Unicode (ISO 10646) as their base character set and allow use of the rich set of symbols within various code pages. While this approach is certainly viable for C and C++, it could cause compatiblity problems.
C and C++ predate Unicode and presently depend on 'common' characters. These characters are mostly the non-national-use characters of the ISO 646 collection of character sets. However, C and C++ do use some ASCII-specific characters. Digraphs and trigraphs enable avoiding these characters when using a non-US variant of ISO 646 or when using other character sets, e.g. EBCDIC.
There are two ASCII characters that are not in use by C and C++,
@ (commercial at)
` (accent grave).
@ is in use by Objective C and Objective C++.
These languages are meta-languages built on top of C and C++.
In fact, other programmers may often need to build meta-languages
on top of C and C++.
Furthermore, they may need to compose two different meta language.
That composition is possible in C and C++ today precisely
because they do not use
I strongly recommend the C and C++
do not use those characters.
No approach above is likely to be sufficient alone. Furthermore, all approaches have the potential to invalidate existing code. If designers investigate potential invalidations and provide migration strategies, we can sanely extend the operator set.
In this section, I outline some changes to operators that I think would improve C and C++.
The prefix dereference operator is a known and persistent problem for programmers. It complicates declaration and expression syntax. It occasionally requires grouping parentheses. It occasionally requires reading expressions inside-out.
Solution is a postfix dereference operator. The only parenthesis required are those for function calls. Declarations (and their corresponding expressions) are read strictly left-to-right, with the exception of the final type.
Once a postifix dereference operator is in place, the prefix dereference operator becomes redundant. It can be removed at some later time. After the prefix dereference operator is gone, that operator space could be reused for another purpose. This timeline leads to the following migration strategy.
|use that meaning|
are natural pairs in the sense that
the second can be rewritten as the first with a constant;
The same is true of one's complement and exclusive or;
However, they don't use the same operator.
They could though,
as there is no current use of
~ as an infix operator.
Indeed, PL/I took this approach
where the operator was the ¬ symbol.
Changing the exclusive or operator to
would free up the infix
^ operator for other purposes.
The most pressing use is exponentiation.
It would not suprise me if most uses of binary
in C/C++ comments is as an exponentiation operator,
not an exclusive or operator.
This leads to the following migration strategy.
|replace uses of token
Standards could either retain or remove token
The migration strategy for removal follows.
|wait||replace uses of token
|remove ||do nothing|
Both of the operator improvements above provide space for two different exponentiation (power) operators.
That space is needed because:
powfunction means different things in different libraries.
powfunction is not a clean mapping from typical mathematical expressions.
powfunction is not a replacement for a integer power operation.
I prefer the
** has the advange of not using national-use characters
and hence needing no lexical work-arounds.
To round out the examples, let me provide some short hints at possibilities.
|exp function |
|logical identity |
|logical xor |
|logical xor |
|absolute value |
This table merges the operators of both C and C++. It extends the precedence levels as necessary to assign a shared consistent level.
||1L scope (C++)||1L scope (C++)|
||3R C-style cast (around type)
0 group (around expression)
|2L functional cast (after type) (C++)
2L call (after expression)
||0 list (a literal in C)||2L functional cast (after type)|
||0 lambda (C++)||2L index|
||3R type attribute|
||3R allocation (C++)|
||3R pre-(in|dec)crement||2L post-(in|dec)crement|
||3R bitwise not|
||3R logical not|
||4L member pointer (C++)|
||3R dereference||5L multiply|
||potential digraph conflict||5L modulo||potential digraph conflict|
||3R promote||6L add|
||3R negate||6L subtract|
? template argument
||8L order||? template argument|
||3R address-of||10L bitwise and|
||11L bitwise xor|
||12L bitwise or|
||13L logical and|
||14L logical or|
||15R conditional (C)
16R conditional (C++)
||16R throw (C++)|
||argument separator (within call)
element separator (within list)
17L sequence (otherwise)