Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

templates

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.

like image 971
Jimmy Avatar asked Aug 17 '11 01:08

Jimmy


People also ask

When did C++ get templates?

Templates were introduced in Release 3.0 of the language, in October 1991.

Are templates supported in C?

The main type of templates that can be implemented in C are static templates. Static templates are created at compile time and do not perform runtime checks on sizes, because they shift that responsibility to the compiler.

During which phase templates are processed in C++?

Instantiation is the process by which a C++ compiler creates a usable function or object from a template. The C++ compiler uses compile-time instantiation, which forces instantiations to occur when the reference to the template is being compiled.

How are templates compiled in C++?

Template compilation requires the C++ compiler to do more than traditional UNIX compilers have done. The C++ compiler must generate object code for template instances on an as-needed basis. It might share template instances among separate compilations using a template repository.


2 Answers

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).

like image 56
John Zwinck Avatar answered Sep 29 '22 13:09

John Zwinck


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!

like image 31
templatetypedef Avatar answered Sep 29 '22 11:09

templatetypedef