2

This compiles:

std::map<int, std::vector<int> > vDescriptorAtom;

This:

std::map<int, std::vector<int>> vDescriptorAtom;

gives the following error:

src/MessageHandler.cpp:191: error: >> should be > > within a nested template argument list

This is obviously because >> is an operator. But looking at the error GCC throws, I cannot understand what it is trying to say. What does "nested template argument list" mean. Thanks.

Chani
  • 5,055
  • 15
  • 57
  • 92
  • Please put error message in title to improve searchability. Once you do, you'll see that it is a dup. – Raymond Chen Oct 23 '13 at 12:24
  • 2
    [Template within template: why "\`>>' should be \`> >' within a nested template argument list"](http://stackoverflow.com/questions/6695261/template-within-template-why-should-be-within-a-nested-template-arg) explains it in its own title. – Raymond Chen Oct 23 '13 at 12:28
  • @RaymondChen It is a similar question but it could be argued they are asking different questions. The dup is asking should it really be ambiguous which is different than what does this mean. – Shafik Yaghmour Oct 23 '13 at 12:38
  • @ShafikYaghmour What do you do if one person asks a question that is answered by the set-up to another person's question? e.g. one person asks "How do I do X?" and another person asks, "I'm using Y to do X, and I have a question..." That's what happened here. The proposed dup says "when we are using template inside another template..." which answers "what does nested template mean?" – Raymond Chen Oct 23 '13 at 13:18
  • @RaymondChen I get what you are saying but the answers to these two question are very different so [the accepted answer here](http://meta.stackexchange.com/questions/12182/when-is-a-duplicate-question-not-a-duplicate) would say they are not a dup while the [one here](http://meta.stackexchange.com/questions/95799/closing-as-duplicate-when-the-answers-are-duplicates) has a different take still leaves room to see them as not dups so I am on the fence on this one. – Shafik Yaghmour Oct 23 '13 at 13:32
  • @RaymondChen Probably, I tried bring this up in another [related topic](http://meta.stackexchange.com/questions/195292/should-we-discourage-marking-c-and-c-questions-as-duplicates-of-each-other-and) on cross tag dups and my perception is that there does not seem to be much interest in discussing the topic. – Shafik Yaghmour Oct 23 '13 at 13:37
  • @ShafikYaghmour I couldn't find guidance on meta on the subject of "What if a question includes an answer in the question that itself answers another question?" (I.e., the answer is in the question, not the answer.) Probably because the words "question" and "answer" are too noisy on meta. – Raymond Chen Oct 23 '13 at 13:39

3 Answers3

4

Stuff between outer pair of brackets (1) is a template argument list for std::map template

std::map<int, std::vector<int>>
// (1)  ^                     ^
// (2)                   ^   ^

The inner pair (2) is template argument list for std::vector template and it's nested inside the first and that's what the error is saying.

Prior to C++11, a whitespace was mandatory between >> at the end to make life easier for the parser (as you observed, it would be interpreted as right shift operator instead). Many compilers made the effort to parse it correctly anyway, in order to give a meaningful error message, proving that's not impossible. I guess that's one of the reasons this restriction was relaxed in C++11.

jrok
  • 54,456
  • 9
  • 109
  • 141
  • 2
    @downvoter I'd like to know the reason for downvote so I can improve the answer. Thank you. – jrok Oct 23 '13 at 12:12
4

You have a nested template argument there:

std::map<int, std::vector<int>>
        ^-outer---------------^
              ^-inner--------^

This is allowed in C++, however in C++ prior to C++11, >> was automatically tokenized to the right-shift-operator, driving usage like in your non-compiling example invalid, because what the compiler sees after tokenization is

[std] [::] [map] [<] [int] [,] [std] [::] [vector] [<] [int] [>>]
                open 1                           open 2           .... close?

i.e., it wouldn't find the corresponding closing >.

This is what the error message is trying to describe, including a proposed fix. Under the hood, this proposal by the compiler means it is actually able to guess what you mean, but by the C++2003 standard, it is not allowed to guess and not allowed to allow that usage.

In current C++ (C++11 and newer), your second usage is allowed.

Sebastian Mach
  • 38,570
  • 8
  • 95
  • 130
  • Hello Miss Downvotress. Would you be kind enough and explain to me unlucky and unknowing scholar what I have done wrong, so I can mend that tarnish in time before the big test? – Sebastian Mach Oct 23 '13 at 12:38
3

Do you have pedantic mode on? It's telling you "you meant to write this". Although newer versions of GCC (4.6...?) and C++11 fix this.

It knows you obviously meant to write > >, but it wouldn't be complying with the standard if it allowed it. Code ought to be portable, that is any C++ compiler ought to work.

You have a template within a template, hence nested. This is fine, it's like List<List<Something<int> > >, a list of lists of somethings of an int.

That is what nested templates mean.

Alec Teal
  • 5,770
  • 3
  • 23
  • 50