C ++模板角括号陷阱 - 什么是C ++ 11修复? [英] C++ Templates Angle Brackets Pitfall - What is the C++11 fix?

查看:136
本文介绍了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>><(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:当解析一个模板参数列表时,第一个非嵌套 > 作为结束分隔符,而不是大于 - 运算符。类似地,第一非嵌套>> 被视为两个连续但不同的> 令牌,第一作为模板参数列表的结尾,并完成模板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修复?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆