Note that there are some explanatory texts on larger screens.

plurals
  1. PODeducing template argument for nested template fails
    text
    copied!<p>Ok, I read throught quite some “could not deduce template argument” questions but none seems to match my case — or I don't understand the answer…</p> <p>There's <a href="https://stackoverflow.com/questions/4630941/failed-to-specialize-function-template" title="Question 4630941">one</a> that I feel goes in the right direction, but I failed extracting the solution for my problem.</p> <p>The stripped down code in my header looks like this:</p> <pre><code>template&lt;typename T&gt; class TemplateProblem { public: // Do I really need this or did I miss something from the STL? template&lt;typename Tin, typename Tout&gt; struct UnaryFunction : public std::unary_function&lt;Tin, Tout&gt; { virtual Tout operator()(Tin input) = 0; }; template&lt;typename Tin, typename Tout&gt; struct StaticCast : public UnaryFunction&lt;Tin, Tout&gt; { virtual Tout operator()(Tin input) { return static_cast&lt;Tout&gt;(input); } }; private: T * const _data; T const _bias; template&lt;typename Tin&gt; void Init(Tin * data, int length, UnaryFunction&lt;Tin, T&gt; mapper, Tin bias); public: template&lt;typename Tin&gt; TemplateProblem(Tin * data, int length, Tin bias = Tin()); template&lt;typename Tin&gt; TemplateProblem(Tin * data, int length, UnaryFunction&lt;Tin, T&gt; mapper, Tin bias = T()); }; template&lt;typename T&gt; template&lt;typename Tin&gt; void TemplateProblem&lt;T&gt;::Init(Tin * data, int length, UnaryFunction&lt;Tin, T&gt; mapper, Tin bias) { T mappedBias = mapper(bias); for (int i = 0; i &lt; length; i++) { _data[i] = data[i] + mappedBias; } } template&lt;typename T&gt; template&lt;typename Tin&gt; TemplateProblem&lt;T&gt;::TemplateProblem(Tin * data, int length, UnaryFunction&lt;Tin, T&gt; mapper, Tin bias = T()) : _data(new T[length]), _bias(bias) { Init(data, length, mapper, bias); } template&lt;typename T&gt; template&lt;typename Tin&gt; TemplateProblem&lt;T&gt;::TemplateProblem(Tin * data, int length, Tin bias = T()) : _data(new T[length]), _bias(bias) { StaticCast&lt;Tin, T&gt; cast; Init(data, length, cast, bias); } </code></pre> <p>I instantiate it like this:</p> <pre><code>unsigned char pixels[] = {23, 42, 65, 97}; TemplateProblem&lt;int&gt; tp(pixels, 4); </code></pre> <p>From VS2012 I get these messages:</p> <pre><code>Error 1 error C2784: 'void TemplateProblem&lt;T&gt;::Init(Tin *,int,TemplateProblem&lt;T&gt;::UnaryFunction&lt;Tin,T&gt;,Tin)' : could not deduce template argument for 'TemplateProblem&lt;T&gt;::UnaryFunction&lt;Tin,T&gt;' from 'TemplateProblem&lt;T&gt;::StaticCast&lt;Tin,Tout&gt;' ...\templateproblem.h 62 1 TemplateProblem Error 2 error C2893: Failed to specialize function template 'void TemplateProblem&lt;T&gt;::Init(Tin *,int,TemplateProblem&lt;T&gt;::UnaryFunction&lt;Tin,T&gt;,Tin)' ...\templateproblem.h 62 1 TemplateProblem </code></pre> <p>The error also occurs when I move the two <code>struct</code>s out of the <code>class</code> as <a href="https://stackoverflow.com/questions/12640808/nested-template-and-parameter-deducing" title="Question 12640808">this answer</a> suggests.</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