C++ template Partial Specialization Member Function; Am I doing right? I'm trying to improve my C++ template metaprogramming skills. From my understanding, I can't partially specialize a function(either member function or not). So, I need to define another class that can be used as template class, which enables us partially specialize template argument.
So, In order to check my understanding, I've wrote a simple code. It uses general template class and partially specialized template class to deal with various types. (I've tested this code using gcc 4.8 / clang 3.4 / MSVC 2015) It would give a result as follows:

Generic
Specialization for std::string
Specialization for bool
Specialization for integral type

Am I doing write using class templates? I need a review for this. Any comment would be helpful.
// a.hpp
#ifndef A_HPP
#define A_HPP
#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;type_traits&gt;

// declaration &amp; implementation for bar_impl::bar
template &lt;typename T, typename T1, typename T2, typename T3&gt;
struct bar_impl
{
    static void bar(const T&amp; input)
     {
        (void)input;
        std::cout &lt;&lt; &quot;Generic&quot; &lt;&lt; std::endl;
    }
};

template &lt;typename T&gt;
struct bar_impl&lt;T, std::false_type, std::true_type, std::false_type&gt;
{
    static void bar(const T&amp; input)
    {
        (void)input;
        std::cout &lt;&lt; &quot;Specialization for integral type&quot; &lt;&lt; std::endl;
    }
};

template &lt;typename T&gt;
struct bar_impl&lt;T, std::false_type, std::true_type, std::true_type&gt;
{
    static void bar(const T&amp; input)
    {
        (void)input;
        std::cout &lt;&lt; &quot;Specialization for bool&quot; &lt;&lt; std::endl;
    }
};

template &lt;typename T, typename T1, typename T2&gt;
struct bar_impl&lt;T, std::true_type, T1, T2&gt;
{
    static void bar(const T&amp; input)
    {
        (void)input;
        std::cout &lt;&lt; &quot;Specialization for std::string&quot; &lt;&lt; std::endl;
    }
};

struct Foo
{
    template &lt;typename T&gt;
    void bar(const T&amp; input);
};

template &lt;typename T&gt;
void Foo::bar(const T&amp; input)
{
    bar_impl&lt;
        T,
        typename std::is_same&lt;T, std::string&gt;::type,
        typename std::is_integral&lt;T&gt;::type,
        typename std::is_same&lt;T, bool&gt;::type
    &gt;::bar(input);
}

#endif // A_HPP

// b.cpp
#include &quot;a.hpp&quot;

int main()
{
  Foo foo;

  foo.bar(Foo());
  foo.bar(std::string());
  foo.bar(bool());
  foo.bar(int());
  return 0;
}
What you've written does work, and you seem to understand all the concepts (e.g., "you can't partially specialize function templates"), and your coding style (indentation and whatnot) seems fine.



Well, one coding-style improvement: You need those (void)input casts to shut up compiler warnings about unused parameters... in C. But in C++ you don't need those casts; what you do instead is, if you're not going to use a parameter, just don't give it a name.

static void bar(const T&amp;) { ... }




However, your code doesn't really match any common TMP patterns — which is probably just because you're noodling around, learning, instead of trying to solve a specific problem.

If you just wanted to produce the same output with less code, you would do this:

struct Foo
{
    template&lt;class T&gt; auto bar(const T&amp;) -&gt; std::enable_if_t&lt;!std::is_integral&lt;T&gt;::value&gt;
    {
        std::cout &lt;&lt; "Generic" &lt;&lt; std::endl;
    }

    template&lt;class T&gt; auto bar(const T&amp;) -&gt; std::enable_if_t&lt;std::is_integral&lt;T&gt;::value&gt;
    {
        std::cout &lt;&lt; "Specialization for integral type" &lt;&lt; std::endl;
    }

    void bar(bool)
    {
        std::cout &lt;&lt; "Specialization for bool" &lt;&lt; std::endl;
    }

    void bar(const std::string&amp;)
    {
        std::cout &lt;&lt; "Specialization for std::string" &lt;&lt; std::endl;
    }
};

int main()
{
    Foo foo;
    foo.bar(Foo());
    foo.bar(std::string());
    foo.bar(bool());
    foo.bar(int());
    return 0;
}


That is, your code is equivalent to providing two overloads of foo.bar, plus a couple of mutually exclusive templates (one for integral types and one for other types). The specific overloads for bool and std::string will take precedence over the templates.



Alternatively, you could use tag dispatch, as follows. (But I don't recommend it in this case because it turns out to be surprisingly subtle. Replace the second instance of std::true_type with ... and watch that overload silently drop out. I'm not actually sure why.)

struct Foo
{
    template&lt;class T&gt; void bar_impl(const T&amp;, ...) {
        std::cout &lt;&lt; "Generic" &lt;&lt; std::endl;
    }

    template&lt;class T&gt; void bar_impl(const T&amp;, std::true_type) {
        std::cout &lt;&lt; "Specialization for integral type" &lt;&lt; std::endl;
    }

    void bar_impl(const std::string&amp;, ...) {
        std::cout &lt;&lt; "Specialization for std::string" &lt;&lt; std::endl;
    }

    void bar_impl(const bool&amp;, std::true_type) {
        std::cout &lt;&lt; "Specialization for bool" &lt;&lt; std::endl;
    }

    template&lt;class T&gt; void bar(const T&amp; input)
    {
        bar_impl(input, std::is_integral&lt;T&gt;{});
    }
};




Maybe the best approach would be to use two non-interacting levels of tag dispatch: first, is T integral? and second, what is T exactly?

template&lt;bool IsIntegral&gt; struct BarImpl {
    template&lt;class T&gt; static void bar(const T&amp;) {
        std::cout &lt;&lt; "Generic" &lt;&lt; std::endl;
    }
    static void bar(const std::string&amp;) {
        std::cout &lt;&lt; "Specialization for std::string" &lt;&lt; std::endl;
    }
};

template&lt;&gt; struct BarImpl&lt;true&gt; {
    template&lt;class T&gt; static void bar(const T&amp;) {
        std::cout &lt;&lt; "Specialization for integral type" &lt;&lt; std::endl;
    }
    static void bar(bool) {
        std::cout &lt;&lt; "Specialization for bool" &lt;&lt; std::endl;
    }
};

struct Foo {
    template&lt;class T&gt; void bar(const T&amp; input) {
        BarImpl&lt;std::is_integral&lt;T&gt;::value&gt;::bar(input);
    }
};