Note that there are some explanatory texts on larger screens.

plurals
  1. POSpecific Template Friendship in C++
    primarykey
    data
    text
    <p>I have a question for Specific Template Friendship in C++. in the book C++ Primer, the specific template friendship is written like this:</p> <pre><code> template &lt;class T&gt; class Foo3; template &lt;class T&gt; void templ_fcn3(const T&amp;); template &lt;class Type&gt; class Bar { // each instantiation of Bar grants access to the // version of Foo3 or templ_fcn3 instantiated with the same type friend class Foo3&lt;Type&gt;; friend void templ_fcn3&lt;Type&gt;(const Type&amp;); // ... }; </code></pre> <p>The special point is that there is </p> <pre><code>&lt;Type&gt; </code></pre> <p>after the class or function name in the <strong>friend</strong> statement.</p> <p>However,in practice, if I write this:</p> <pre><code>template &lt;class Type&gt; class T_CheckPointer; template &lt;class T&gt; T_CheckPointer&lt;T&gt; operator+(const T_CheckPointer&lt;T&gt; &amp;, const size_t n); template &lt;typename Type&gt; class T_CheckPointer { // Specific Template Friendship friend T_CheckPointer&lt;Type&gt; operator+ &lt;Type&gt; (const T_CheckPointer&lt;Type&gt; &amp;, const size_t n); // other code... } </code></pre> <p>There will be a error during instantiations for the template function.</p> <p>And if I change </p> <pre><code>// Specific Template Friendship friend T_CheckPointer&lt;Type&gt; operator+ &lt;Type&gt; (const T_CheckPointer&lt;Type&gt; &amp;, const size_t n); </code></pre> <p>to</p> <pre><code>// Specific Template Friendship friend T_CheckPointer&lt;Type&gt; operator+ &lt;&gt; (const T_CheckPointer&lt;Type&gt; &amp;, const size_t n); </code></pre> <p>by deleting the word <strong>type</strong> after the function name, then all will be okay.</p> <p>Anyone can tell me the reason?</p> <hr> <p>For information, there is the <strong>error message</strong> when I call</p> <pre><code>int iarr[] = {1, 2, 3, 4}; T_CheckPointer&lt;int&gt; itcp(iarr, iarr+4); </code></pre> <p>error message:</p> <pre><code>/usr/include/c++/4.4/bits/stl_iterator_base_types.h: In instantiation of ‘std::iterator_traits&lt;int&gt;’: /usr/include/c++/4.4/bits/stl_iterator.h:96: instantiated from ‘std::reverse_iterator&lt;int&gt;’ ../Classes/T_CheckPointer.hpp:31: instantiated from ‘T_CheckPointer&lt;int&gt;’ ../PE16.cpp:520: instantiated from here /usr/include/c++/4.4/bits/stl_iterator_base_types.h:127: error: ‘int’ is not a class, struct, or union type /usr/include/c++/4.4/bits/stl_iterator_base_types.h:128: error: ‘int’ is not a class, struct, or union type /usr/include/c++/4.4/bits/stl_iterator_base_types.h:129: error: ‘int’ is not a class, struct, or union type /usr/include/c++/4.4/bits/stl_iterator_base_types.h:130: error: ‘int’ is not a class, struct, or union type /usr/include/c++/4.4/bits/stl_iterator_base_types.h:131: error: ‘int’ is not a class, struct, or union type </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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