How to express a string literal within a template parameterized by the type of the characters used to represent the literal?
<p>Consider the following simple map:</p>

<pre><code>class MyCoolMap : public unordered_map&lt;const char *, const char *&gt;
{
public:
  ProtoTypeMap()
  {
    insert(value_type("in1", "out1"));
    insert(value_type("in2", "out2"));
    ...
    insert(value_type("inN", "outN"));
  }
};
</code></pre>

<p>Now, suppose I need to make this map available both for <code>char</code> and <code>wchar_t</code> strings. So, I rewrite it as follows:</p>

<pre><code>template&lt;class C&gt;
class MyCoolMap : public unordered_map&lt;const C *, const C *&gt;
{
public:
  MyCoolMap()
  {
    insert(value_type("in1", "out1"));
    insert(value_type("in2", "out2"));
    ...
    insert(value_type("inN", "outN"));
  }
};
</code></pre>

<p>And, of course, this does not work for <code>C=wchar_t</code>. The problem is that I do not know how to template the difference between <code>char</code> literals and <code>wchar_t</code> literals. Right now I see two solutions, both ugly.</p>

<p><strong>Solution 1</strong> - specialize <code>MyCoolMap</code> by <code>wchar_t</code>:</p>

<pre><code>template&lt;&gt;
class MyCoolMap&lt;wchar_t&gt; : public unordered_map&lt;const wchar_t *, const wchar_t *&gt;
{
public:
  MyCoolMap()
  {
    insert(value_type(L"in1", L"out1"));
    insert(value_type(L"in2", L"out2"));
    ...
    insert(value_type(L"inN", L"outN"));
  }
};
</code></pre>

<p>This is bad, because the whole logic is duplicated.</p>

<p><strong>Solution 2</strong> - a traits like solution:</p>

<pre><code>#define _TOWSTRING(x) L##x
#define TOWSTRING(x) _TOWSTRING(x)

template &lt;class C, int&gt; struct special_string;
#define DECL_SPECIAL_STRING(STR) \
const int ss_##STR = __LINE__; \
template&lt;&gt; struct special_string&lt;char, ss_##STR&gt; { static const char *get_value() { return #STR; } }; \
template&lt;&gt; struct special_string&lt;wchar_t, ss_##STR&gt; { static const wchar_t *get_value() { return TOWSTRING(#STR); } };

DECL_SPECIAL_STRING(in1)
DECL_SPECIAL_STRING(out1)
DECL_SPECIAL_STRING(in2)
DECL_SPECIAL_STRING(out2)
...
DECL_SPECIAL_STRING(inN)
DECL_SPECIAL_STRING(outN)

template&lt;class C&gt;
class MyCoolMap : public unordered_map&lt;const C *, const C *&gt;
{
public:
  MyCoolMap()
  {
#define INSERT_MAPPING(in, out) insert(value_type(special_string&lt;C, ss_##in&gt;::get_value(), special_string&lt;C, ss_##out&gt;::get_value()))
    INSERT_MAPPING(in1, out1);
    INSERT_MAPPING(in2, out2);
    ...
    INSERT_MAPPING(inN, outN);
#undef INSERT_MAPPING
  }
};
</code></pre>

<p>This way I do not need to replicate the logic, but this is so verbose and relies heavily on macros.</p>

<p>There must be a better way; I just do not see it. </p>

<p>I am using VS2010.</p>

<p><strong>EDIT</strong></p>

<p>I am glad that a much simpler solution is proposed - the credits go to <a href="http://stackoverflow.com/users/5987/mark-ransom">http://stackoverflow.com/users/5987/mark-ransom</a>. I had to make minor fixes to make it compile, though:</p>

<pre><code>#define _TOWSTRING(x) L##x
#define TOWSTRING(x) _TOWSTRING(x)

template&lt;typename C&gt; const C * ChooseCW(const char * c, const wchar_t * w);
template&lt;&gt; const char * ChooseCW&lt;char&gt;(const char * c, const wchar_t * w)
{
  return c;
}
template&lt;&gt; const wchar_t *ChooseCW&lt;wchar_t&gt;(const char * c, const wchar_t * w)
{
  return w;
}

#define CW(C, STR) ChooseCW&lt;C&gt;(#STR, TOWSTRING(#STR))
</code></pre>

<p>Thanks again.</p>
