Namespaces
Variants
Views
Actions

Angle Bracket hack

From cppreference.com
< cpp‎ | language
Revision as of 20:33, 16 May 2020 by Jeffythedragonslayer (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


The angle bracket hack resolves a lexing ambiguity when a templated name ending in ">" is used as the template parameter of another class template or function template. Prior to C++11, when two consecutive angle brackets appeared, many lexers would interpret these as the right shift operator instead according to the language grammar. As a workaround, a space would need to be inserted between the two angle brackets so they will lexer can interpret these as individual angle bracket tokens.

template <typename T>
struct A
{
};
 
template <typename T>
struct B
{
};
 
 
int main()
{
	A<B<int> > c; // space between >'s needed on many compilers prior to C++11
	A<B<int>> c;  // ok on C++11 and above