C++ 模板角括号陷阱 - C++11 修复是什么?

C++ Templates Angle Brackets Pitfall - What is the C++11 fix?(C++ 模板角括号陷阱 - C++11 修复是什么?)
本文介绍了C++ 模板角括号陷阱 - C++11 修复是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

在 C++11 中,这是现在有效的语法:

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?

我认为像 myTemplate< 这样的形式x>>3 > 不是问题,因为您可以通过执行 myTemplate<(x>>3)> 来消除它们的歧义.

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

推荐答案

通过在解析模板参数时在解析规则中添加特殊情况来修复.

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

C++11 14.2/3: 当解析一个template-argument-list时,第一个非嵌套的>被当作结束分隔符而不是一个大于运算符.类似地,第一个非嵌套的 >>> 被视为两个连续但不同的 > 标记,其中第一个被视为 的结尾template-argument-list 并完成template-id.

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.

这篇关于C++ 模板角括号陷阱 - C++11 修复是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

What do compilers do with compile-time branching?(编译器如何处理编译时分支?)
Can I use if (pointer) instead of if (pointer != NULL)?(我可以使用 if (pointer) 而不是 if (pointer != NULL) 吗?)
Checking for NULL pointer in C/C++(在 C/C++ 中检查空指针)
Math-like chaining of the comparison operator - as in, quot;if ( (5lt;jlt;=1) )quot;(比较运算符的数学式链接-如“if((5<j<=1)))
Difference between quot;if constexpr()quot; Vs quot;if()quot;(“if constexpr()之间的区别与“if())
C++, variable declaration in #39;if#39; expression(C++,if 表达式中的变量声明)