Draw an ASCII checkerboard I'm a newcomer to C++, from C#. I wrote a program that prints an ASCII checkerboard with a side length of the number passed to it (link to original Code Golf question).

stdafx.h

#include "targetver.h"

#include &lt;stdio.h&gt;
#include &lt;tchar.h&gt;

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


Checkers.cpp

#include "stdafx.h"

using std::cin;
using std::cout;
using std::endl;
using std::stoi;
using std::string;
using std::stringstream;

string header(int num);

template &lt;bool white&gt;
string body(const string &amp;head, int num);

template &lt;bool white&gt;
stringstream&amp; append_body(stringstream &amp;stream, int rem, int approx);

int _tmain(int argc, _TCHAR* argv[])
{
    int num;

    if (argc == 1 || ((num = stoi(argv[1])) &lt; 0))
    {
        cout &lt;&lt; "Usage: ./Checkers [nonnegative int]" &lt;&lt; endl;
        return 1;
    }

    switch (num)
    {
    case 0:
        return 0;
    case 1:
        cout &lt;&lt; "+--+" &lt;&lt; endl &lt;&lt; "|  |" &lt;&lt; endl &lt;&lt; \
            "|  |" &lt;&lt; endl &lt;&lt; "+--+" &lt;&lt; endl;
        return 0;
    }

    int rem = num % 2;
    int approx = num - rem;

    const string&amp; head = header(num);
    string&amp; wtail = body&lt;true&gt;(head, num);
    string&amp; btail = body&lt;false&gt;(head, num);

    string&amp; block = wtail + btail;

    stringstream stream;

    for (int i = 0; i &lt; approx / 2; i++)
        cout &lt;&lt; block;

    if (rem == 1)
        cout &lt;&lt; wtail;

    cout &lt;&lt; head &lt;&lt; endl;

#ifdef _DEBUG
    cin.get();
#endif
}

string header(int num)
{
    string result;

    for (int i = 0; i &lt; num; i++)
        result += "+--";

    return result + '+';
}

template &lt;bool white&gt;
string body(const string &amp;head, int num)
{
    int rem = num % 2;
    int approx = num - rem;

    stringstream stream;
    stream &lt;&lt; head &lt;&lt; endl;

    append_body&lt;white&gt;(stream, rem, approx) &lt;&lt; endl;
    append_body&lt;white&gt;(stream, rem, approx) &lt;&lt; endl;

    return stream.str();
}

template &lt;bool white&gt;
stringstream&amp; append_body(stringstream &amp;stream, int rem, int approx)
{
    const char *const block =
        white ? "|  |##" : "|##|  ";

    for (int i = 0; i &lt; approx / 2; i++)
        stream &lt;&lt; block;

    stream &lt;&lt; '|';

    if (rem == 1)
        stream &lt;&lt; (white ? "  |" : "##|");

    return stream; // not a local so this is OK
}




Questions:


I intentionally had a little fun optimizing prematurely here, i.e. generating the header ("+--+") and body ("|##|  |") strings only once instead of every loop. What could I do to increase readability without getting rid of each-loop optimizations like this?
When is it okay to use non-type template parameters, like I did in body? This means that you always have to rewrite your code when you want to offer a non-template alternative, correct?
To appreciate a power of non-type templates, try to implement body and append_body in plain C. You will see immediately why your approach is an abuse of templates.
Most important

body() returns a temporary object:

    stringstream stream;
    ...
    return stream.str();


and clang expectedly complains:

error: non-const lvalue reference to type
  'basic_string&lt;[3 * ...]&gt;' cannot bind to a temporary of type
  'basic_string&lt;[3 * ...]&gt;'
string&amp; wtail = body&lt;true&gt;(head, num);
        ^       ~~~~~~~~~~~~~~~~~~~~~