Is this ->> an old operator or a typo/error?
C++COperatorsHistoryC++ Problem Overview
In the course of my reading I came accross WG14 Defect Report #51 written in 1993 (or perhaps 1893, they left off the century and millennium). In the code sample there, apparently an operator spelled ->>
is used on a pointer to a struct
. I can't find it in any operator precedence tables I've found, so I am wondering, is or was it ever an operator, and if so, what does (or did, as the case may be) this operator do?
At first I thought it was a typo, but it is reproduced twice more in the text and another time in the code sample in the response to the question, and I have a hard time believing it just slipped past at least two C experts without being noticed, when it jumped out at a novice like me. It's also at the focal point of the code, very easy to notice, and was never corrected.
Here is the code with added indentation:
#include <stdlib.h>
struct A {
char x[1];
};
main()
{
struct A *p = (struct A *) malloc(sizeof(struct A) + 100);
p->>x[5] = '?'; /* This is the key line [for both them and us] */
return 0;
}
I tried to compile this code with both a C and C++ compiler and it failed to parse in either one. Perhaps this was some operator in an early version of C that isn't used any more?
This feels suspiciously like the What is the name of this operator: "-->"? question, but I don't think this is a combination of two other operators, I don't see how it can be divided up and be valid.
C++ Solutions
Solution 1 - C++
It looks like a problem in the transcription process. There is a similar problem in DR 42, where the greater than sign is doubled: http://www.open-std.org/jtc1/sc22/wg14/docs/rr/dr_042.html
Solution 2 - C++
I learned C in 1992, and I'm 100% certain there was no such operator back then.
From the context, p->>x[5]
, we can deduce that it appears to do exactly the same thing as the more familiar arrow operator, ->
. It is therefore likely to be a typo.
Alternatively, it could be an encoding issue in transcribing the code into HTML. If you look at the source to that page, you can see it's got a strange mixture of escape codes and literal <
and >
characters:
<TT><B>#include <stdlib.h><BR>
Solution 3 - C++
This does seem likely to have been a transcription error, but I think it would be useful to write out how a real C compiler would interpret this construct, anyway, just to make clear that it isn't a clever trick. The first thing it's important to know is this sentence, from C11 §6.5.4p4 (technically, N1570; this language is unchanged since C89, although the section number was probably different; emphasis mine):
> If the input stream has been parsed into preprocessing tokens up to a given character, the next preprocessing token is the longest sequence of characters that could constitute a preprocessing token.
That means the six-character string " p->>x"
must be tokenized as p -> > x
, not p - >> x
or p - > > x
. (It doesn't actually matter in this case, it would be a syntax error either way, but this rule can be the difference between a program parsing as intended, and not; the standard gives the example x+++++y
, which is interpreted as x++ ++ +y
, not as x++ + ++y
, even though only the latter is a well-formed expression.)
The next thing to know is simply that the right-hand argument of the ->
operator must be an identifier, per the grammar rules for postfix-expression in §6.5.2. Obviously >
isn't an identifier, so we have a definite syntax error.