19

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?

I consider that forms like myTemplate< x>>3 > are a non-problem, since you can disambiguate them by doing myTemplate<(x>>3)>.

5
  • 1
    > is already a token but the parser is and was greedy. The fix must therefore look different. – One possibility would of course be to make >> not be a token. Apr 3, 2013 at 11:01
  • 2
    I guess you're looking for §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. "
    – Zeta
    Apr 3, 2013 at 11:02
  • 2
    "what is the fix that the standard uses to allow the first version" - I believe this has nothing to do with The standard. I mean - the implementation. I believe it's a compiler's decision how to implement this requirement, forced by The standard. Apr 3, 2013 at 11:03
  • 3
    @KirilKirov: the standard has changed the rules for tokenizing C++ source. From the POV of the authors of the standard, this is the "fix" that they made. It's up to the implementer how to write code to match the new (more context-sensitive) tokenizing rules. Apr 3, 2013 at 11:24
  • @KonradRudolph see the rephrasing for my second question here: stackoverflow.com/questions/15785496#comment22443479_15785583 (comment on Mike Seymour's answer).
    – Norswap
    Apr 3, 2013 at 11:31

1 Answer 1

24

It's fixed by adding a special case to the parsing rules when parsing template arguments.

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.

11
  • 2
    That looks like a defect. IIRC, the intent was that something like template <int i> class X{}; X<(10 >> 2)> (with the extra parentheses) would be legal. Apr 3, 2013 at 11:21
  • 3
    @JamesKanze Isn' that covered by the "non-nested" part?
    – Norswap
    Apr 3, 2013 at 11:27
  • 3
    @Norswap I missed that part. In context, I would expect "nested" to refer to <...> type brackets only, but that doesn't really make sense, so it must be. Apr 3, 2013 at 12:46
  • 1
    @Norswap That's an interesting idea. Currently no (unary or binary) operator consists of more than one token. In the larger context of expressions, however, something like std::vector consists of three tokens, but works like a single element. (I think using two tokens for a single operator would cause problems in a recursive descent parser. It would be interesting to knock up the grammar of C++ expressions in yacc, and see if changing it to use two '>' tokens as a single operator causes conflicts. Apr 3, 2013 at 12:49
  • 1
    @Norswap: "What I suggest shouldn't pose any problem that I can see." But it's not enough that you (or anyone else) can't immediately see any problems. To change a fundamental aspect of the grammar (that an operator is a token), with potential ramifications throughout the language, you'd need to do a lot of work to prove that you haven't caused any problems. A self-contained special case is a lot easier to verify, even if it does feel ugly. Apr 3, 2013 at 13:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.