C++ reduce repetitions of code for similar classes I have the following two classes to make appending strings - and wstrings respectively - easier:

#include &lt;string&gt;
#include &lt;sstream&gt;

class StringBuilder
{
public:
    template &lt;typename T&gt;
    StringBuilder&amp; operator&lt;&lt;(const T&amp; t)
    {
        this-&gt;stream &lt;&lt; t;
        return *this;
    }

    operator std::string() const;

private:
    std::ostringstream stream;
};

StringBuilder::operator std::string() const
{
    return this-&gt;stream.str();
}

class WStringBuilder
{
public:
    template &lt;typename T&gt;
    WStringBuilder&amp; operator&lt;&lt;(const T&amp; t)
    {
        this-&gt;stream &lt;&lt; t;
        return *this;
    }

    explicit operator std::wstring() const;

private:
    std::wstringstream stream;
};

WStringBuilder::operator std::wstring() const 
{
    return this-&gt;stream.str();
}


Now, as you see, pretty much all of the code (except for the concrete string and stringstream types) is repeated, especially the operators.

The question is:


Is there any way to reduce the duplicated code? I thought about a base class, templates and specialization, but the problem is I don't know how to determine the correct stringstream type depending on the string type then.
Yes, there is. In fact, a very nice way to solve this code redundancy issue comes to mind if you consider that std::string is a just type alias for std::basic_string&lt;char&gt; and std::wstring an alias for std::basic_string&lt;wchar_t&gt;. Likewise, std::stringstream and std::wstringstream are also just aliases for std::basic_stringstream&lt;char&gt; and std::basic_stringstream&lt;wchar_t&gt;, respectively. This allows for an easy template-based approach to the problem.

What really suggests itself to me is keeping this alias-of-template-base-type pattern from the STL (although there may be other valid approaches as well), an implementation of which could look something like this:

#include &lt;sstream&gt;
#include &lt;string&gt;

namespace impl {

    template &lt;typename CharType&gt;
    class StringBuilderImpl {

        std::basic_stringstream&lt;CharType&gt; stream;

        public:
        template &lt;typename T&gt;
        StringBuilderImpl&amp; operator&lt;&lt;(T const&amp; t) {
            stream &lt;&lt; t;
            return *this;
        }

        operator std::basic_string&lt;CharType&gt;() const {
            return stream.str();
        }
    };
}

using StringBuilder = impl::StringBuilderImpl&lt;char&gt;;
using WStringBuilder = impl::StringBuilderImpl&lt;wchar_t&gt;;


StringBuilderImpl corresponds somewhat to std::basic_[string/stringstream] and takes the underlying character type as a template argument. Your former classes StringBuilder and WstringBuilder are now only aliases for certain template specializations, just as std::[string/wstring] and std::[stringstream/wstringstream] are.

(In my code, StringBuilderImpl is kind of hidden away inside an impl namespace. If it serves your need, however, you could also make it part of the public interface of your program library, allowing further specifications such as StringBuilderImpl&lt;char16_t&gt;)