What is the meaning of `???-` in C++ code? [duplicate]
Asked Answered
C

3

67

I saw the following code from some legacy codes:

 size_t a = 1 ???- 2 :0;

What does the symbol ???- mean in C++? How should I understand it?

Christachristabel answered 21/5, 2013 at 4:43 Comment(12)
??? must only be used in comments. :)Rice
It'd be fun to come up with a fake meaning for this operator. ???- could be the 'Really??? I'm shocked!' operator, a ? that hints to the compiler that the first branch is rare to be taken, for example.Acuate
Obligatory WTF operator mention: ??!??!Alesandrini
@Acuate Fun for you. Frustration to those who read your code. :PRice
@Mark Garcia Yeah, it'd only be a joke, like --> operator :)Acuate
Trigraphs exist to solve two problems. Firstly some embedded systems only support a very limited character set and may not be able to display or enter characters like "^" and "~". Secondly some OSes support multiple code pages which have different encodings for "~","^","|" etc. the only way to ensure your code is supports all the code page variations is to use trigraphs(Heres looking at you IBM EBCDIC!).Bandur
@JamesAnderson, Funny how I keep spazzing at code that assumes A-Z is contiguous, but not code that doesn't use trigraphs, eh?Alesandrini
@JamesAnderson Embedded systems? What are you smoking? Trigraphs don't affect the program functionality or target system. They are effectively a character encoding for source files and a convenience for users missing certain keys on their keyboards. IBM lobbies against their deprecation because, supposedly, it allows them to avoid forcing their mainframe users to standardize on an EBCDIC text encoding. But a mainframe is the exact opposite of an embedded system.Impeachable
@Impeachable -- I meant the development environment could not support these characters. These days you mostly cross compile from a bigger "normal" machine, but, in olden times you often had to use custom hardware.Bandur
@Alesandrini -- or "0" < "a", or '2' - 30 will result in an integer of value 2.Bandur
@MarkGarcia Just a shift away from ??/, which translates to a backslash, which extends a comment to the next line…Impeachable
This question gets asked about once every two weeks, why has it not been closed yet!?Surrogate
N
109

It's actually:

size_t a = 1 ? ~2 :0;

??- is a trigraph for ~


Trigraphs are from an old era... before some of us were even born.

Back in the days, there were some characters that weren't always supported. An unknowing programmer would try to type in such a character only to find that it doesn't exist on the keyboard!

enter image description here
Image Source: http://www.myoldmac.net/cgi-data/forum/phpBB2/viewtopic.php?t=305

So trigraphs were added to allow the programmer to access the functionality of these characters when they didn't exist (either in the encoding or from the keyboard).

Nowadays, they are obsolete and are more effective in confusing the reader than in getting around old standards.

So either that code is really old, or the author was being a jerk.

Nudism answered 21/5, 2013 at 4:44 Comment(7)
I saw the notification come up like 10 milliseconds before I clicked and I couldn't do anything about it :pAlesandrini
Thank you! This is weird to me though.Christachristabel
I vote for (b), the author was being a jerk. a = 1 ? ~2 : 0 is obfuscated long hand for a = ~2. The trinary operator is there for one reason only: To obfuscate the code. Adding the trigraph to obfuscate the tilde just ices the cake.Chalybite
"Trigraphs are from an old era." -- Trigraphs were added to the language by the 1989 ANSI C standard. They've never been removed; the 2011 ISO C standard still has them.Monograph
If the author was not a jerk and the trigraph was really needed in that situation, then it would have been written as ? ??- instead of ???- for easier understanding.Pascoe
And as a little addendum: the c++ committee standard planned to remove them in the past 5 years. However, there has been some opposition and they still remain.Artie
@Pascoe Actually I think if the author cared for readability, it'd have been ? compl 2 instead of ? ??- 2.Repulse
I
16

??- is a trigraph for the tilde ~ character; the line is equivalent to:

size_t a = 1 ? ~2 :0;
Investigation answered 21/5, 2013 at 4:45 Comment(0)
B
9

??- is a trigraph for ~ character. Some other trigraphs are:

 ??= for #

??/ for \

??' for ^

??! for |

The usage of trigraphs are very rare now.

Boathouse answered 21/5, 2013 at 9:55 Comment(2)
"The usage of trigraphs are very rare now." - not in SO.Rice
@Yawz I'd think they'd go with ~ over ??- if only for the character counting.Redheaded

© 2022 - 2024 — McMap. All rights reserved.