Those who dealt with template classes and template of template classes may have faced compilation error due to right angle bracket ( >> ) even the syntax appeared to be correct.
For e.g if we take the following code snippet
// This is not valid syntax in standard C++. std::vector<std::vector<int>> incorrect; // Adding whitespace between the brackets yields the correct parse. std::vector<std::vector<int> > correct;
If you look at the OK syntax you can see a space between the ending right angle brackets. We may wonder we had given the right syntax and compiler did not interpret it well. In C++ as you know the notation for right shift operator is also similar – ( >> ). This actually confuses the compiler. In computer programming this is called Maximal Munch
In C++ 0x, there were a proposal to remove this inconvenience and the committee has accepted it. Visual C++ 2010 compiler implemented this as standard.
Now we can simply declare without worrying about maximal munch of C++ compiler.
// This is a valid syntax in standard C++ 0x. std::vector<std::vector<int>> correct;
Let’s take bit more complex example
template<int i> class X { /* ... */ };
X< 1>2 > x1; // Syntax error.
X<(1>2)> x2; // Okay.
template<class T> class Y { /* ... */ };
Y<X<1>> x3; // same as "Y<X<1> > x3;".
Y<X<6>>1>> x4; // Syntax error
Y<X<(6>>1)>> x4;".// . Instead, write like this. right shift operator is properly interpreted.
The new implementation may break some of the existing source code as well.
std::cout << (Y<X< 1>>::c >::c>::c) << '\n'; // C++ 98 source
The above code is perfectly valid in C++ 98 standard and it’s supposed to do right shift after constant “1″ but as per the new standard this may treat differently and give different output. Right usage of paranthesis is a solution to avoid these kind of issues. You can see more details about this proposal and implications here.