Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There seem to be several issues with your code, one of which looks like a VS10 bug.</p> <ol> <li>You're calling <code>T::func()</code> from <code>A</code> without casting <code>A</code> to <code>T</code>, this is needed as part of CRTP since <code>A</code> doesn't derive from <code>T</code>. -- FixL <code>return static_cast&lt;T*&gt;(this)-&gt;func();</code></li> <li>What you're passing to <code>decltype</code> looks like a static function invocation while <code>func</code> is in fact an instance function. Since <code>decltype</code> <strong>doesn't</strong> actually run the function you should do something like this <code>decltype(static_cast&lt;T*&gt;(nullptr)-&gt;func())</code></li> <li><code>func</code> is private in <code>B</code> and can't be called from <code>A</code> -- Fix: change <code>A</code> to be a <code>struct</code></li> <li>This looks like a bug in VS10, even after all these fixes I get an error that you're trying to use an undefined class <code>B</code> in the <code>decltype</code>.</li> </ol> <p>As a workaround can you refactor <code>func</code> out into a base class? (now we need two template parameters, one for casting to and one for <code>decltype</code> thus creating a new idiom CRTPEX)</p> <pre><code>struct Base { void func() { } }; template&lt;typename T, typename U&gt; struct A { auto func() -&gt; decltype(static_cast&lt;T*&gt;(nullptr)-&gt;func()) { return static_cast&lt;U*&gt;(this)-&gt;func(); } }; struct B : public A&lt;Base, B&gt;, public Base { }; </code></pre> <hr> <p>I see that g++ also chokes on this <code>decltype</code> can anyone confirm that this is a defect? If so I will open a bug for Microsoft. It is my understanding that the following code is valid but neither g++ nor VC10 compile it.</p> <pre><code>template&lt;typename T&gt; struct A { auto func() -&gt; decltype(static_cast&lt;T*&gt;(nullptr)-&gt;func()) { return static_cast&lt;T*&gt;(this)-&gt;func(); } }; struct B : public A&lt;B&gt; { void func() {} }; </code></pre>
 

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