Hello,
There is a syntax erron in the following code -- adjacent ">"s need a delimiter (it is marked AAAA)
template <class T> class A {};
template <class T> class B {};
int
main() {
A<B<int>>(); // AAAA
}
But VC8 can compile it. It seems to be a error but on the other hand Mr. Stroustrup have being dreaming on such a syntax for ages and he promotes it pretty vigorously now, as far as know. So, I am just curious if it is the bug or a favor
--
Michael Kochetkov

On adjacent ">"s in a parameterized declaration
jian
Being technically correct and providing elaboration to get others on the same page are entirely two different things.
Could you elaborate on the "longest token sequence rule" (citation ) and how it would be used to resolve this ambiguity I'm thinking that you're referring to the "longest token" rule used to resolve ambiguity in lexical analysis (not parsing). e.g. a=+++j is lexically analyzed using "++" as the third token, since it is longer than the alternative "+". (http://www.technologyforall.com/TechForAll/visualC.html) In my limited experience in parsing, its my belief that the parser decides semantics (makes a reduction) based on the tokens it has seen so far (on the token stack) along with 1 or 2 look-ahead tokens (it does not receive the entire token sequence all at once); this limits the amount of information a parser has to resolve ambiguities.
However, turns out that 1 look-ahead can resolve the ambiguity in your example. But should it That I don't know...
I did check the feedback page; that's when I learned that the new behavior of allowing >> to close off a nested template is by design (so my statement "probably a bug" is incorrect). I'm still watching to see what JonCaves says about the ambiguity issue that it caused.
It would also be interesting to learn how the lexer/parser has changed to deal with this. My guess is that VC7 lexer probably treated ">>" as a single token, and in VC8, ">>" are two tokens and the parser decides which parse tree applies.
Brian
Matthew McDonald
> Could you log the issue at http://lab.msdn.microsoft.com/productfeedback/default.aspx
OK. But you would probably have to get it back in a while :).
--
Michael Kochetkov
hozdemir
> Yeah, I finally realized that also (thanks). Too bad the original poster chose
> the option of being opaque and argumentative rather than reexplaining in more detail.
Hmm... I have explained it the most technically correct way I know: "A C++ pareser shall consider the longest tokens' sequence as a declaration. The quote you cite is not." My dictionary gives the following meaning of "argumentative" -- likes to argue. I do not argue -- there is nothing to argue about. And of course I believe I was not opaque. I do not know how it would be possible to misinterpret my cited words above. My English is probably not that easy to understand but my C++ shall be pretty well.
> I do agree that the semantic of Michael's example has changed between VC7
> and VC8. I wonder if the C++ standards committee has taken this into consideration.
You probably want to check the feedback page I have given above. Why guess .. They have explained everything.
--
Michael Kochetkov
d_a_heitbrink
A<B<(10>>1)> >()
It's easier to see how it makes sense.
sa acc
Are you sure that is valid The compiler gives:
error C2143: syntax error : missing ';' before 'constant'
which is occuring because the parser accepts A<B<10>> and is looking for either an identifier or a semicolon to follow it, but it finds 1. This is an expected diagnostic.
Brian
iama.NETer
> What does A<B<10>>1> >(); mean
A<B<5> >();
> Note that the right most two brackets are unmatched.
They are matched indeed.
--
Michael Kochetkov
Sushovan De
I don't mean to spar, but I don't understand how your example can be correct code.
What does A<B<10>>1> >(); mean
Note that the right most two brackets are unmatched.
Brian
NYY
Yeah, I finally realized that also (thanks). Too bad the original poster chose the option of being opaque and argumentative rather than reexplaining in more detail.
There are other cases where the compiler requires parenthesis in order to disambiguate a valid expression from an invalid one. I am not a C++ language lawyer, but I believe that the compiler is not always bound to disambiguate tokens solely on the condition that a particular choice leads to a syntax error.
Case in point:
int a = 1,2;is a syntax error, while
int a = (1,2);
is valid, meaning a=2.
I do agree that the semantic of Michael's example has changed between VC7 and VC8. I wonder if the C++ standards committee has taken this into consideration.
Brian
Stathread01
Probably a bug. Forgiving incorrect C++ syntax is not a favor if it means non-portable code.
MilwaukeeCharlie
Could you log the issue at http://lab.msdn.microsoft.com/productfeedback/default.aspx
Thanks in advance for taking the time to log it.
Thanks, Ayman Shoukry VC++ TeamSealed
> Forgiving incorrect C++ syntax is not a favor if it means non-portable code.
It looks like in this very case it is: http://lab.msdn.microsoft.com/ProductFeedback/viewFeedback.aspx feedbackId=FDBK46141
--
Michael Kochetkov
jpguest
And it looks like they have failed -- pretty valid code stopped working:
template <class T> class A {};
template <int> class B {};
int
main() {
A<B<10>>1> >();
}
--
Michael Kochetkov
Abhiram Khune MSFT
> Could you elaborate on the "longest token sequence rule" (citation )
> and how it would be used to resolve this ambiguity
I believe I can.
A parser shall consider the code
A<B<10>>1> >();
the following way:
1. Can "A" to be a part of a declaration Yes.
2. Can "A<" to be a part of a declaration Yes.
3. Can "A<B" to be a part of a declaration Yes.
4. Can "A<B<" to be a part of a declaration Yes.
5. Can "A<B<" to be a part of a declaration Yes.
6. Can "A<B<10" to be a part of a declaration Yes.
7. Can "A<B<10" to be a part of a declaration Yes.
8. Can "A<B<10>" to be a part of a declaration Yes.
9. Can "A<B<10>>" to be a part of a declaration Yes. It can be because we can treat two '>' tokens in a row as single '>>' token.
BTW in terms of new VC8 rules it is probably the end of decl-specifier-seq of a declaration but "longest token sequence rule" (citation ) (yes, it is a citation. I cite 7.1/2) makes us to go further.
10. Can "A<B<10>>1>" to be a part of a declaration Yes.
11. Can "A<B<10>>1> " to be a part of a declaration Yes.
12. Can "A<B<10>>1> >" to be a part of a declaration Yes.
13. Can "A<B<10>>1> >(" to be a part of a declaration No.
Well, we have gotten the longest declaration (the decl-specifier-seq precisely) on the twelveth step.
HTH
--
Michael Kochetkov
smsm_fcis
> Are you sure that is valid The compiler gives:
Yes, of course I am.
> error C2143: syntax error : missing ';' before 'constant'
It shall not be that way.
> which is occuring because the parser accepts A<B<10>> and is
> looking for either an identifier or a semicolon to follow it, but it finds 1.
A C++ pareser shall consider the longest tokens' sequence as a declaration. The quote you cite is not.
> This is an expected diagnostic.
Of course this is not. It is obvious bug.
--
Michael Kochetkov