<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Simple question: Why cannot deduce template argument?]]></title><description><![CDATA[<p>The cross product accepts matrix of N vectors, each has dimentionality N+1:</p>
<pre><code class="language-cpp">template&lt;int N, class T&gt; inline Vector&lt;N+1,T&gt; CrossProduct(Matrix&lt;N, Vector&lt;N+1,T&gt; const&gt; const &amp;m)
{ 
	...
}
</code></pre>
<p>When I call it, I get &quot;cannot deduce template argument for T&quot; compiler error:</p>
<pre><code class="language-cpp">Vector&lt;3,double&gt; r = CrossProduct( Matrix&lt;2,Vector&lt;3,double&gt; const&gt;( ... ) );
</code></pre>
<p>How to fix it? I do not like to write template parameters each time:</p>
<pre><code class="language-cpp">Vector&lt;3,double&gt; r = CrossProduct&lt;2,double&gt;( Matrix&lt;2,Vector&lt;3,double&gt; const&gt;( ... ) );
</code></pre>
<p>Edit: surprisingly, it can work with overload:</p>
<pre><code class="language-cpp">template&lt;class T&gt; inline Vector&lt;3,T&gt; CrossProduct(Matrix&lt;2, Vector&lt;3,T&gt; const&gt; const &amp;m)
{ 
	...
}

...

Vector&lt;3,double&gt; r = CrossProduct( Matrix&lt;2,Vector&lt;3,double&gt; const&gt;( ... ) ); //No error here
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/261255/simple-question-why-cannot-deduce-template-argument</link><generator>RSS for Node</generator><lastBuildDate>Mon, 07 Sep 2026 13:27:23 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/261255.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 15 Feb 2010 19:38:01 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Simple question: Why cannot deduce template argument? on Mon, 15 Feb 2010 19:48:11 GMT]]></title><description><![CDATA[<p>The cross product accepts matrix of N vectors, each has dimentionality N+1:</p>
<pre><code class="language-cpp">template&lt;int N, class T&gt; inline Vector&lt;N+1,T&gt; CrossProduct(Matrix&lt;N, Vector&lt;N+1,T&gt; const&gt; const &amp;m)
{ 
	...
}
</code></pre>
<p>When I call it, I get &quot;cannot deduce template argument for T&quot; compiler error:</p>
<pre><code class="language-cpp">Vector&lt;3,double&gt; r = CrossProduct( Matrix&lt;2,Vector&lt;3,double&gt; const&gt;( ... ) );
</code></pre>
<p>How to fix it? I do not like to write template parameters each time:</p>
<pre><code class="language-cpp">Vector&lt;3,double&gt; r = CrossProduct&lt;2,double&gt;( Matrix&lt;2,Vector&lt;3,double&gt; const&gt;( ... ) );
</code></pre>
<p>Edit: surprisingly, it can work with overload:</p>
<pre><code class="language-cpp">template&lt;class T&gt; inline Vector&lt;3,T&gt; CrossProduct(Matrix&lt;2, Vector&lt;3,T&gt; const&gt; const &amp;m)
{ 
	...
}

...

Vector&lt;3,double&gt; r = CrossProduct( Matrix&lt;2,Vector&lt;3,double&gt; const&gt;( ... ) ); //No error here
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1856163</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1856163</guid><dc:creator><![CDATA[SAn]]></dc:creator><pubDate>Mon, 15 Feb 2010 19:48:11 GMT</pubDate></item><item><title><![CDATA[Reply to Simple question: Why cannot deduce template argument? on Mon, 15 Feb 2010 20:02:01 GMT]]></title><description><![CDATA[<p>SAn schrieb:</p>
<blockquote>
<p>The cross product accepts matrix of N vectors, each has dimentionality N+1:</p>
<pre><code class="language-cpp">template&lt;int N, class T&gt; inline Vector&lt;N+1,T&gt; CrossProduct(Matrix&lt;N, Vector&lt;N+1,T&gt; const&gt; const &amp;m)
{ 
	...
}
</code></pre>
<p>When I call it, I get &quot;cannot deduce template argument for T&quot; compiler error:</p>
<pre><code class="language-cpp">Vector&lt;3,double&gt; r = CrossProduct( Matrix&lt;2,Vector&lt;3,double&gt; const&gt;( ... ) );
</code></pre>
</blockquote>
<p>That is not a context, in which N can be deduced (14.8.2.4/14) - in short, because a non-type template parameter cannot be deduced from an expression (here: N+1). One solution would be to use different non-type parameters and then filter using sfinae, if the conditions are no satisfied:</p>
<pre><code class="language-cpp">template&lt;int M, int N, class T&gt; inline typename enable_if_c&lt;M==N+1,Vector&lt;M,T&gt; &gt;::type CrossProduct(Matrix&lt;N, Vector&lt;M,T&gt; const&gt; const &amp;m)
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1856181</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1856181</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Mon, 15 Feb 2010 20:02:01 GMT</pubDate></item><item><title><![CDATA[Reply to Simple question: Why cannot deduce template argument? on Mon, 15 Feb 2010 20:20:22 GMT]]></title><description><![CDATA[<p>Thank you!</p>
<p>I have done this way:</p>
<pre><code class="language-cpp">template&lt;int N, class V&gt; inline typename enable_if_c&lt;V::dim==N+1,V&gt;::type CrossProduct(Matrix&lt;N, V const&gt; const &amp;m)
</code></pre>
<p>Where vector contain declaration to get it dimention:</p>
<pre><code class="language-cpp">template&lt;int N, class T&gt; class Vector {public: static int const dim = N; ... };
</code></pre>
<p><strong>camper</strong>, your knowledge is as you are one of the developers of the C++ standard <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f44d.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--thumbs_up"
      title=":+1:"
      alt="👍"
    /> .</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1856199</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1856199</guid><dc:creator><![CDATA[SAn]]></dc:creator><pubDate>Mon, 15 Feb 2010 20:20:22 GMT</pubDate></item><item><title><![CDATA[Reply to Simple question: Why cannot deduce template argument? on Tue, 16 Feb 2010 17:41:40 GMT]]></title><description><![CDATA[<p>SAn schrieb:</p>
<blockquote>
<p><strong>camper</strong>, your knowledge is as you are one of the developers of the C++ standard <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f44d.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--thumbs_up"
      title=":+1:"
      alt="👍"
    /> .</p>
</blockquote>
<p>not quite.<br />
If you wonder why such contexts cannot be used to deduce a nontype template eargument, consider what it would mean to allow these cases. It would either require to specify the exact kinds of expressions that could be used or to require the implementor to include an equation solver in the compiler:</p>
<pre><code class="language-cpp">template &lt;int N&gt; void foo(int (&amp;)[N*N]); // N positive or negative ?
</code></pre>
<p>is already not quite as simple as an addition.<br />
Either solution would increase compiler complexity while the posible gain is doubtful. With functions one could almost always get away with enable_if. And if we wanted to partially specialise a class template some kind of indirection should be possible in general.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1856724</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1856724</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Tue, 16 Feb 2010 17:41:40 GMT</pubDate></item></channel></rss>