Note that there are some explanatory texts on larger screens.

plurals
  1. POTemplate partial specialization with multiple template argument error
    primarykey
    data
    text
    <p>When I use template partial specialization on a class with one template argument, I can specialize a method like this:</p> <pre><code>#include &lt;cstdlib&gt; template&lt; std::size_t Dim &gt; class Test { public: int foo(); }; template&lt; std::size_t Dim &gt; inline int Test&lt; Dim &gt;::foo() { return 0; } template&lt;&gt; inline int Test&lt; 1 &gt;::foo() { return 1; } int main() { Test&lt; 2 &gt; wTest2; Test&lt; 1 &gt; wTest1; wTest2.foo(); wTest1.foo(); return 0; } </code></pre> <p>The method foo is specialized for Dim = 1. But as soon as I add a template argument to my class, like this:</p> <pre><code>#include &lt;cstdlib&gt; template&lt; typename T, std::size_t Dim &gt; class Test { public: int foo(); }; template&lt; typename T, std::size_t Dim &gt; inline int Test&lt; T, Dim &gt;::foo() { return 0; } template&lt; typename T &gt; inline int Test&lt; T, 1 &gt;::foo() { return 1; } int main() { Test&lt; double, 2 &gt; wTest2; Test&lt; double, 1 &gt; wTest1; wTest2.foo(); wTest1.foo(); return 0; } </code></pre> <p>The compiler (of VS2010) complains with these errors:</p> <pre><code>1&gt;c:\documents and settings\cayouette\my documents\codelocal\testtemplatespecialization\main.cpp(20): error C3860: template argument list following class template name must list parameters in the order used in template parameter list 1&gt;c:\documents and settings\cayouette\my documents\codelocal\testtemplatespecialization\main.cpp(20): error C2995: 'int Test&lt;T,Dim&gt;::foo(void)' : function template has already been defined 1&gt; c:\documents and settings\cayouette\my documents\codelocal\testtemplatespecialization\main.cpp(7) : see declaration of 'Test&lt;T,Dim&gt;::foo' 1&gt;c:\documents and settings\cayouette\my documents\codelocal\testtemplatespecialization\main.cpp(20): error C2976: 'Test&lt;T,Dim&gt;' : too few template arguments 1&gt;c:\documents and settings\cayouette\my documents\codelocal\testtemplatespecialization\main.cpp(26): error C2264: 'Test&lt;T,Dim&gt;::foo' : error in function definition or declaration; function not called 1&gt; with 1&gt; [ 1&gt; T=double, 1&gt; Dim=2 1&gt; ] 1&gt;c:\documents and settings\cayouette\my documents\codelocal\testtemplatespecialization\main.cpp(27): error C2264: 'Test&lt;T,Dim&gt;::foo' : error in function definition or declaration; function not called 1&gt; with 1&gt; [ 1&gt; T=double, 1&gt; Dim=1 1&gt; ] 1&gt; 1&gt;Build FAILED. </code></pre> <p>The way I see this, there is no ambiguity and the compiler should be able to resolve everything and work just like the one argument case.</p> <p>If this is not supported in C++, please explain why.</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.
 

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