对于嵌套模板,`>>` 何时成为标准 C++(而不是`> >`)

For nested templates, when did `gt;gt;` become standard C++ (instead of `gt; gt;`)?(对于嵌套模板,`gt;gt;` 何时成为标准 C++(而不是`gt; gt;`)?)
本文介绍了对于嵌套模板,`>>` 何时成为标准 C++(而不是`> >`)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我似乎记得,在过去,在处理嵌套模板参数时被警告不要将两个 > 字符放在彼此旁边(没有空格).我什至依稀记得声明了向量的向量并遇到了这个编译错误.

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

我的问题是:

这个约定在什么时候成为可以接受的做法?

At what point did this convention become an acceptable practice?

它是标准 C++ 的一部分吗?

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.

推荐答案

使用嵌套 >>> 关闭的模板是 正式支持 即将推出的标准 C++0x(现在是 C++11).以前,您需要空间或编译器为您加倍努力(并且做了标准未指明的事情).

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

问题源于C中的>>是右移运算符,它是一个单独的词法标记,与两个单独的>冲突code> 在经典构造的 C++ 编译器的解析阶段需要的标记(并且仅在模板的情况下,而不是在它实际上是右移时).换句话说,>>,如果允许关闭嵌套模板,在词法上是模棱两可的,但是这可以(并且正在)在解析过程中通过额外的复杂性来解决(这在现代 C++ 中实际上是没什么新鲜的).

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

这篇关于对于嵌套模板,`>>` 何时成为标准 C++(而不是`> >`)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

相关文档推荐

How do compilers treat variable length arrays(编译器如何处理变长数组)
Deduce template argument from std::function call signature(从 std::function 调用签名推导出模板参数)
check if member exists using enable_if(使用 enable_if 检查成员是否存在)
Standard Library Containers with additional optional template parameters?(具有附加可选模板参数的标准库容器?)
Uses of a C++ Arithmetic Promotion Header(C++ 算术提升标头的使用)
Parameter pack must be at the end of the parameter list... When and why?(参数包必须位于参数列表的末尾...何时以及为什么?)