<p>Can you specialize a template method within a template class without specializing the class template parameter?</p>

<p>Please note that the specialization is on the <em>value</em> of the template parameter, not its type.</p>

<p>This seems to compile under Visual Studio 2008 SP1 complier, but not GCC 4.2.4.</p>

<pre><code>#include &lt;iostream&gt;
using namespace std;
template &lt;typename T&gt;
class A
{
private:
    template &lt;bool b&gt;
    void testme();

    template &lt;&gt;
    void testme&lt;true&gt;() { cout &lt;&lt; "true" &lt;&lt; endl; };

    template &lt;&gt;
    void testme&lt;false&gt;() { cout &lt;&lt; "false" &lt;&lt; endl; };

public:
    void test();
};

template&lt;typename T&gt; struct select {};
template&lt;&gt; struct select&lt;int&gt;    { static const bool value = true; };
template&lt;&gt; struct select&lt;double&gt; { static const bool value = false; };

template &lt;class T&gt;
void A&lt;T&gt;::test() { testme&lt;select&lt;T&gt;::value&gt;(); }

int main(int argc, const char* argv[])
{
    A&lt;int&gt;      aInt;
    A&lt;double&gt;   aDouble;

    aInt.test();
    aDouble.test();

    return 0;
}
</code></pre>

<p>GCC tells me:"error: explicit specialization in non-namespace scope ‘class A’"</p>

<p>If it is not supported in the standard, can anyone tell me why?</p>

[{'answer_id': 64824, 'author': 'moswald', 'author_id': 8368, 'author_profile': 'https://Stackoverflow.com/users/8368', 'pm_score': 2, 'selected': False, 'text': "<p>It is not supported in the standard (and it is apparently a known bug with Visual Studio that you <em>can</em> do it).</p>\n\n<p>The standard doesn't allow an inner template (member function <em>or</em> class) to be specialized without the outer template being specialized as well.  One of the reasons for this is that you can normally just overload the function:</p>\n\n<pre><code>template&lt;typename ty&gt;\nclass A\n{\npublic:\n      void foo(bool b);\n      void foo(int i);\n};\n</code></pre>\n\n<p>Is equivalent to:</p>\n\n<pre><code>template&lt;typename ty&gt;\nclass A\n{\npublic:\n   template&lt;typename ty2&gt;\n   void foo(ty2);\n\n   template&lt;&gt;\n   void foo(bool b);\n\n   template&lt;&gt;\n   void foo(int i);\n};\n</code></pre>\n"}, {'answer_id': 64884, 'author': 'Tyler', 'author_id': 3561, 'author_profile': 'https://Stackoverflow.com/users/3561', 'pm_score': 0, 'selected': False, 'text': "<p>I've never heard of that being possible; it would make sense to me if it was <em>not</em> supported by all compilers.  So here is an idea for a workaround:</p>\n\n<p>Implement a template function outside of your class which takes the same action as the method.  Then you can specialize this function, and it call it from the method.  Of course, you'll also have to pass in any member variables that it needs (and pointers thereto if you want to modify their values).</p>\n\n<p>You could also create another template class as a subclass, and specialize that one, although I've never done this myself and am not 100% sure it would work.  (Please comment to augment this answer if you know whether or not this second approach would work!)</p>\n"}, {'answer_id': 67478, 'author': 'Bronek', 'author_id': 10042, 'author_profile': 'https://Stackoverflow.com/users/10042', 'pm_score': 2, 'selected': True, 'text': '<p>Here is another workaround, also useful when you need to partialy specialize a function (which is not allowed). Create a template functor class (ie. class whose sole purpose is to execute a single member function, usually named operator() ), specialize it and then call from within your template function.</p>\n\n<p>I think I learned this trick from Herb Sutter, but do not remember which book (or article) was that. For your needs it is probably overkill, but nonetheless ...</p>\n\n<pre><code>template &lt;typename T&gt;\nstruct select;\n\ntemplate &lt;bool B&gt;\nstruct testme_helper\n{\n  void operator()();\n};\n\ntemplate &lt;typename T&gt;\nclass A\n{\nprivate:\n  template &lt;bool B&gt; void testme()\n  {\n    testme_helper&lt;B&gt;()();\n  }\n\npublic:\n  void test()\n  {\n    testme&lt;select&lt;T&gt;::value&gt;();\n  }\n};\n\ntemplate&lt;&gt; void testme_helper&lt;true&gt;::operator()()\n{\n  std::cout &lt;&lt; "true" &lt;&lt; std::endl;\n}\n\ntemplate&lt;&gt; void testme_helper&lt;false&gt;::operator()()\n{\n  std::cout &lt;&lt; "false" &lt;&lt; std::endl;\n}\n</code></pre>\n'}, {'answer_id': 275212, 'author': 'Johannes Schaub - litb', 'author_id': 34509, 'author_profile': 'https://Stackoverflow.com/users/34509', 'pm_score': 2, 'selected': False, 'text': '<p>here is how you do it:</p>\n\n<pre><code>template&lt;typename A&gt;\nstruct SomeTempl {\n    template&lt;bool C&gt; typename enable_if&lt;C&gt;::type \n    SomeOtherTempl() {\n        std::cout &lt;&lt; "true!";\n    }\n\n    template&lt;bool C&gt; typename enable_if&lt;!C&gt;::type \n    SomeOtherTempl() {\n        std::cout &lt;&lt; "false!";\n    }\n};\n</code></pre>\n\n<p>You can get <code>enable_if</code> from my other answer where i told them how to check for a member function\'s existance in a class using templates. or you can use boost, but remember to change <code>enable_if</code> to <code>enable_if_c</code> then.</p>\n'}]
['https://Stackoverflow.com/questions/64498', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/8524/']