For nested templates, when did `>>` become standard C++ (instead of `> >`)?
Asked Answered
H

4

29

I seem to recall, in times of yore, being warned against putting two > characters right next to each other (without a space) when dealing with nested template parameters. I even vaguely remember declaring vectors of vectors of whatever and encountering this compilation error.

But now I find that there is absolutely nothing wrong with compiling the dreaded >>...

My question(s) are thus:

At what point did this convention become an acceptable practice?

Is it part of standard C++?

Was it always part of the standard and the compilers I used (and the professors I had) in college just didn't support it yet?

Maybe these questions are a tad bit historical, but for me it seems that proper historical context makes actual remembering trivial.

Hardden answered 17/8, 2011 at 1:28 Comment(2)
It's part of C++0x/C++11, see #5771631Portillo
I'm guessing the original (if it was a standard) reason was to avoid confusion with the operator, though I don't know enough about the history of the language to adequately answer.Christoperchristoph
M
27

Templates closed with nested >> are officially supported by the upcoming standard C++0x (now C++11). Previously you would need the space, or a compiler that went the extra mile for you (and did things not indicated by the standard).

The issue stems from the fact that >> in C is the right-shift operator, which is a single lexical token, which conflicts with the two separate > tokens that would be needed during the parsing stage in a classically-constructed C++ compiler (and only in the case of templates, not when it actually is a right-shift). In other words, the >>, if allowed to close nested templates, is lexically ambiguous, but this can be (and is being) addressed by extra sophistication during parsing (which in modern C++ is really nothing new).

Morgan answered 17/8, 2011 at 1:31 Comment(2)
So what is the fix from C++11? Could it be as simple as simply making >a token instead of >>. If not, why does this simple fix not work?Godderd
The fix is up to the various implementors of C++11 compilers and other tools. But in general, yes, perhaps one fix for some implementations might be to count >> as two lexical tokens and then combine them during parsing when appropriate. C++11 itself doesn't require any particular method be used to make it work, however.Morgan
H
3

The double angle bracket syntax in templates is still illegal in C++, but some compilers (notably Visual Studio) allow it anyway. In C++0x, which was just ratified and is awaiting publication, this has been corrected.

In short, it is still not legal C++, but will be soon. Sme compilers allow it, but since not all do you should still put the spaces in the angle brackets. In two or three years, you won't need to so this anymore.

Hope this helps!

Horan answered 17/8, 2011 at 1:31 Comment(1)
You may want to update this answer now that C++11 is out. :)Hypocrite
D
1

C++0x. You could take a look at this question of mine for more information.

Disingenuous answered 17/8, 2011 at 1:33 Comment(0)
V
1

It was not in the standard (until very recently -- a few days ago).

Why?

One Word: Efficiency.

Disallowing that make the grammar context free. This make the compiler a little bit faster. In the good old days, every bit counts. Now, with the advancement of both algorithm and hardware, nobody want this trouble.

Vibrator answered 17/8, 2011 at 1:44 Comment(4)
C++'s grammar has not been context free for quite some time.Hypocrite
@Hypocrite it take time to change. The committee don't say "we are not context free now, why not change every bit of the language over night?"Vibrator
C++ and C have never been context-free that I know of. Just typedef is enough to ensure that.Godderd
typedef is handled through "the lexer hack" together with a context-free (LALR(1)) grammar; parsing C++ with a LALR(1) grammar is usually considered impossible (except for the PhD thesis on Fog discussed here: lambda-the-ultimate.org/node/2158).Labor

© 2022 - 2024 — McMap. All rights reserved.