Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It seems that in your particular example nothing in <code>fcn</code> function actually depends on class <code>A</code>. It doesn't even need to access any of the A's methods/fields, neither public nor protected/private. So it doesn't make sense. It would have made some sense otherwise, but at any rate it seems like it is worth re-thinking your problem and come up with a cleaner solution that does not need a hack like that. If, after a deep thought, you still believe you need it, you can do something like this:</p> <pre><code>#include &lt;cstdio&gt; template&lt;typename T&gt; typename T::member_type fcn(const T &amp; v) { return v.value_; } template&lt;class T&gt; class A { public: typedef T member_type; friend member_type fcn&lt; A&lt;T&gt; &gt;(const A&lt;T&gt; &amp;); A() : value_(1986) {} private: T value_; }; int main() { A&lt;int&gt; a; printf("The value is: %d\n", fcn(a)); } </code></pre> <p>Notable thing in the above example is that you need to de-couple a cross dependency and make your free-function not depend on a declaration of class <code>A</code>. If you still feel like you need that coupling, the following code works, too:</p> <pre><code>#include &lt;cstdio&gt; template &lt;typename T&gt; class A; template &lt;typename T&gt; typename A&lt;T&gt;::member_type fcn(const A&lt;T&gt; &amp; v) { return v.value_; } template &lt;typename T&gt; class A { public: typedef int member_type; friend member_type fcn&lt;T&gt;(const A&lt;T&gt; &amp;); A() : value_(1986) {} private: member_type value_; }; int main() { A&lt;void&gt; a; printf("The value is: %d\n", fcn(a)); } </code></pre> <p>Hope it helps. Good Luck!</p>
    singulars
    1. This table or related slice is empty.
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      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