<p>A friend and I were discussing C++ templates. He asked me what this should do:</p>

<pre><code>#include &lt;iostream&gt;

template &lt;bool&gt;
struct A {
    A(bool) { std::cout &lt;&lt; "bool\n"; }
    A(void*) { std::cout &lt;&lt; "void*\n"; }
};

int main() {
    A&lt;true&gt; *d = 0;
    const int b = 2;
    const int c = 1;
    new A&lt; b &gt; (c) &gt; (d);
}
</code></pre>

<p>The last line in main has two reasonable parses. Is 'b' the template argument or is <code>b &gt; (c)</code> the template argument? </p>

<p>Although, it is trivial to compile this, and see what we get, we were wondering what resolves the ambiguity?</p>

[{'answer_id': 52515, 'author': 'Ben Collins', 'author_id': 3279, 'author_profile': 'https://Stackoverflow.com/users/3279', 'pm_score': 0, 'selected': False, 'text': "<p>The greediness of the lexer is probably the determining factor in the absence of parentheses to make it explicit.  I'd guess that the lexer isn't greedy.</p>\n"}, {'answer_id': 52617, 'author': 'Leon Timmermans', 'author_id': 4727, 'author_profile': 'https://Stackoverflow.com/users/4727', 'pm_score': 4, 'selected': True, 'text': "<p>AFAIK it would be compiled as <code>new A&lt;b>(c) > d</code>. This is the only reasonable way to parse it IMHO. If the parser can't assume under normal circumstances a > end a template argument, that would result it much more ambiguity. If you want it the other way, you should have written:</p>\n\n<pre><code>new A&lt;(b &gt; c)&gt;(d);\n</code></pre>\n"}, {'answer_id': 52875, 'author': 'Lee Baldwin', 'author_id': 5200, 'author_profile': 'https://Stackoverflow.com/users/5200', 'pm_score': 2, 'selected': False, 'text': "<p>The C++ standard defines that if for a template name followed by a <code>&lt;</code>, the <code>&lt;</code> is always the beginning of the template argument list and the first non-nested <code>&gt;</code> is taken as the end of the template argument list.</p>\n\n<p>If you intended that the result of the <code>&gt;</code> operator be the template argument, then you'd need to enclose the expression in parentheses. You don't need parentheses if the argument was part of a <code>static_cast&lt;&gt;</code> or another template expression. </p>\n"}, {'answer_id': 71706, 'author': 'Richard Corden', 'author_id': 11698, 'author_profile': 'https://Stackoverflow.com/users/11698', 'pm_score': 3, 'selected': False, 'text': '<p>As stated by Leon &amp; Lee, 14.2/3 (C++ \'03) explicitly defines this behaviour.</p>\n\n<p>C++ \'0x adds to the fun with a similar rule applying to <code>&gt;&gt;</code>.  The basic concept, is that when parsing a template-argument-list a non nested <code>&gt;&gt;</code> will be treated as two distinct <code>&gt;</code> <code>&gt;</code> tokens and not the right shift operator:</p>\n\n<pre><code>template &lt;bool&gt;\nstruct A {\n  A(bool);\n  A(void*);\n};\n\ntemplate &lt;typename T&gt;\nclass C\n{\npublic:\n  C (int);\n};\n\nint main() {\n    A&lt;true&gt; *d = 0;\n    const int b = 2;\n    const int c = 1;\n    new C &lt;A&lt; b  &gt;&gt;  (c) &gt; (d); // #1\n    new C &lt;A&lt; b &gt; &gt;  (c) &gt; (d); // #2\n}\n</code></pre>\n\n<p>\'#1\' and \'#2\' are equivalent in the above.</p>\n\n<p>This of course fixes that annoyance with having to add spaces in nested specializations:</p>\n\n<pre><code>C&lt;A&lt;false&gt;&gt; c;  // Parse error in C++ \'98, \'03 due to "right shift operator"\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/52506', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4798/']