問題描述
我似乎記得,在過去,在處理嵌套模板參數時被警告不要將兩個 >
字符放在彼此旁邊(沒有空格).我什至依稀記得聲明了向量的向量并遇到了這個編譯錯誤.
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++(而不是`> >`)?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!