Nested templates vs shift operator
Asked Answered
B

4

7

I have been reading all around about be aware >> as ending of nested template and >> as shift operator...

Now I have tried it in my MSVS2010 and no problem occured.

std::map<int, std::pair<int, int>> m;

This code works exactly what I want (map of pairs) but I supposed to get some error about >>

Compiler is smarter these days?

Beeman answered 24/4, 2011 at 14:39 Comment(0)
O
7

MSVC++2010 supports C++0x feature Right Angle Brackets

Oscan answered 24/4, 2011 at 14:42 Comment(0)
B
9

Be careful because previously good C++03 code may break with compilers supporting this feature.

MyArray< MyArray<int, 16 >> 2>, 5 > arrayInst;

This would be the fix:

MyArray< MyArray<int, (16 >> 2)>, 5 > arrayInst;
Bangup answered 24/4, 2011 at 15:6 Comment(1)
We all would have been better off had they used (T) instead of <T> in the first place.Selfeffacement
O
7

MSVC++2010 supports C++0x feature Right Angle Brackets

Oscan answered 24/4, 2011 at 14:42 Comment(0)
A
4

This code works exactly what I want (map of pairs) but I supposed to get some error about >>

C++0x has fixed this. So if you're not getting any error with MSVS2010, then its no wonder, as MSVS2010 has implemented some of C++0x features.

Also, even with C++03, many compilers handle such cases, though not required by the Standard(2003).

Abduct answered 24/4, 2011 at 14:41 Comment(9)
Actually, it's not allowed for a conforming C++03 compiler to implement C++0x semantics for >>. There are valid C++03 constructs which can break with this change.Bangup
@Charles: Interesting. But I would like to know what valid C++03 constructs can be broken with this change, and how C++0x handles this then?Abduct
@Nawaz: E.g. shift expressions in constant expressions for non-type template parameters. E.g. tmpl< tmpl<2 >> 1> > instance;. Usually valid C++03 becomes a compile error in C++0x. I haven't managed to construct any situation where this causes a silent change in behaviour.Bangup
@Charles: How does C++0x handle this then? Is it going to interpret >> in <2 >> 1> as shift operator?Abduct
@Nawaz: As I indicated, it's a compile error. >> closes the template, leaving a non-sensical 1> > instance; token sequence where a declarator is needed.Bangup
@Charles: In other words, I can say that few valid C++03 things are invalid in C++0x, right?Abduct
@Nawaz: I'm not 100% sure I know what you are driving. Are you looking to make a wider generalisation? There are a few things that are valid C++03 and aren't valid C++0x. Not least there are several more keywords that can no long be used as ordinary identifiers.Bangup
@Charles: Are you looking to make a wider generalisation?... what does it mean? Are we discussing some philosophy? :-/Abduct
@Nawaz: You said "I can say that few valid C++03 things are invalid in C++0x, right?", my thoughts were that you could say that but we really only touched on one corner case and there a probable many others.Bangup
T
3

C++0x now supports that syntax without errors. Compilers have already started to implement most of these features, so it wouldn't be surprising that the latest Microsoft C++ compiler supports it.

Tradespeople answered 24/4, 2011 at 14:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.