Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is the rationale behind the syntax chosen to declare template friends?
    text
    copied!<p>Declaring template function friends involves some incredibly unintuitive syntax, even for C++! What is the rationale behind the choice of syntax for the extra <code>&lt;&gt;</code> needed? Wouldn't it make more sense to use the <code>template</code> keyword?</p> <p>For those that don't know about this, here is an example of what you might try to do:</p> <pre><code>template &lt;typename T&gt; class Foo { int x; friend void bar(Foo&lt;T&gt;); }; template &lt;typename T&gt; void bar(Foo&lt;T&gt; f) { std::cout &lt;&lt; f.x; } </code></pre> <p>If you try to call <code>bar(Foo&lt;T&gt;())</code>, you will get linker errors.</p> <p>To solve this, you have to forward declare <code>bar</code> (and therefore <code>Foo</code>) and then stick an oddly placed <code>&lt;&gt;</code> in the friend declaration.</p> <pre><code>template &lt;typename T&gt; class Foo; template &lt;typename T&gt; void bar(Foo&lt;T&gt;); template &lt;typename T&gt; class Foo { int x; friend void bar&lt;&gt;(Foo&lt;T&gt;); // note the &lt;&gt; (!?) }; template &lt;typename T&gt; void bar(Foo&lt;T&gt; f) { std::cout &lt;&lt; f.x; } </code></pre> <p>My question is, what is the rationale behind the <code>&lt;&gt;</code> syntax? Wouldn't it be more intuitive to use the <code>template</code> keyword or something along those lines?</p> <p>EDIT: To clarify, <strong>I already know <em>why</em> the &lt;> is required, what I want to know is why they chose to use &lt;> to disambiguate it instead of some other more intuitive syntax.</strong></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload