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

让人想犯罪 __ 提交于 2019-11-26 11:30:28

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/15785496/c-templates-angle-brackets-pitfall-what-is-the-c11-fix

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!