Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Templates Angle Brackets Pitfall - What is the C++11 fix?

In C++11, this is now valid syntax:

vector<vector<float>> MyMatrix;

whereas previously, it had to be written like this (notice the space):

vector<vector<float> > MyMatrix;

My question is what is the fix that the standard uses to allow the first version?

Could it be as simply as making > a token instead of >>? If that's not it, what does not work with this approach?

I consider that forms like myTemplate< x>>3 > are a non-problem, since you can disambiguate them by doing myTemplate<(x>>3)>.

like image 737
Norswap Avatar asked Apr 03 '13 10:04

Norswap


People also ask

What are angle brackets in C?

angular brackets are used for global use of the header files which are predefined and we include in our program. When you use angle brackets, the compiler searches for the file in the include path list. Angular brackets are used for standard inclusions.

Are there templates 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.


1 Answers

It's fixed by adding a special case to the parsing rules when parsing template arguments.

C++11 14.2/3: When parsing a template-argument-list, the first non-nested > is taken as the ending delimiter rather than a greater-than operator. Similarly, the first non-nested >> is treated as two consecutive but distinct > tokens, the first of which is taken as the end of the template-argument-list and completes the template-id.

like image 63
Mike Seymour Avatar answered Oct 19 '22 06:10

Mike Seymour