C++0x step by step: context sensitive shift operator >>

With C++0x, the >> operator has become context sensitive. If you don’t use templates you may wonder what the point is, but this is a very useful little feature that makes things a bit more readable and will save you many compiler errors if you use templates.


If you look at my previous C++ post, you’ll see that I declared a template variable like this:


vector < MyData < int> > data;


You’ll notice that there is a space between the ‘>’ symbols. This is because in previous versions of the standard, 2 consecutive ‘>’ symbols will always be considered to be a shift operator. It would be perfectly natural for the > symbols to be placed right next to each other. It would be more readable, and certainly more intuitive. In the new standard, this has been fixed. >> will now be interpreted context sensitive. If they are part of a template declaration, they are considered to be 2 individual symbols. If they are part of an expression, they are considered to be the shift operator. So from now on, you can do the following and it will finally compile cleanly.


vector < MyData < int>> data;


It is indeed a small thing, but anyone working with templates for a while has bumped into this more than oncce.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>