title: Simplifying the creation of wchar_t/char templated functions using type traits
body: <p>I've been writing a win32 filesystem library and I decided that rather than use TCHAR, I wanted to write a template (header only) library that would work on char/wchar_t irrespective of compiler wide/narrow options.</p>
  
  <p>This left me with two problems:</p>
  
  <ol>
  <li>My library would have to transparently/efficiently work out whether to call the ANSI/Wide version of windows functions (E.g. CreateFileA or CreateFileW) depending on how the template was expanded.</li>
  <li>My library would sometimes need to use string literals, so I needed a way of translating a "string literal into either "" or L"" without creating a runtime cost or confusing the logic.</li>
  </ol>
  
  <p>I've created what I think is a reasonably elegant solution to these two problems but I wanted the stack overflow community to tell me if there are any hidden costs/nasties associated with them.</p>
  
  <p>Firstly (to solve 1) since my template class has T as the character type, I created the following templates/macros in a "detail" namespace within my class:</p>
  
  <pre><code>// Selector template to choose between W and A versions of win32 functions
  template&lt;typename WF, typename AF&gt; inline WF&amp; select_w32func(AF &amp;,        WF &amp; pFuncW, wchar_t) { return pFuncW; }
  template&lt;typename WF, typename AF&gt; inline AF&amp; select_w32func(AF &amp; pFuncA, WF &amp;,        char)    { return pFuncA; }
  template&lt;typename T&gt; inline T get_traits() { return NULL; }
  
  #define SELECT_W32FUNC(x)    auto x = detail::select_w32func(::##x##A, ::##x##W, detail::get_traits&lt;T&gt;());
  #define SELECT_W32FUNCS(x,y) auto x = detail::select_w32func(::x, ::y, detail::get_traits&lt;T&gt;());
  </code></pre>
  
  <p>Next (to solve 2) I created the following templates and macro:</p>
  
  <pre><code>template&lt;typename WC, typename AC&gt; inline const WC* select_chartrait(const AC*,   const WC* b, wchar_t) { return b; }
  template&lt;typename WC, typename AC&gt; inline const AC* select_chartrait(const AC* a, const WC*,   char)    { return a; }
  template&lt;typename WC, typename AC&gt; inline const WC select_chartrait(const AC,     const WC b,  wchar_t) { return b; }
  template&lt;typename WC, typename AC&gt; inline const AC select_chartrait(const AC a,   const WC,    char)    { return a; }
  
  // Macro which allows string literals to be adapted by template parameter T
  #define _S(x) detail::select_chartrait((x), (L##x), detail::get_traits&lt;T&gt;())
  </code></pre>
  
  <p>This allows me to write the constructors and member functions of my class almost as if I wasn't dealing with templated parameters, for example, the following constructor, constructs a w32file object with a CSIDL special folder identifier as it's parent:</p>
  
  <pre><code>basic_w32file(DWORD dwCSIDL, std::basic_string&lt;T&gt; &amp; child) : m_name( MAX_PATH, ' ' )
  {
    SELECT_W32FUNC(PathCombine);
    SELECT_W32FUNCS(strlen, wcslen);
  
    basic_w32file&lt;T&gt; parent( getSystemDirectory ( dwCSIDL, _S(' ')));
    if (child == _S(".") || child == _S(".."))
      PathCombine(&amp;m_name[0], parent.getAbsoluteName().c_str(), child.c_str());
    else
      PathCombine(&amp;m_name[0], parent.getPath().c_str(), child.c_str());
  
    m_name.resize( strlen(&amp;m_name[0]) );
  }
  </code></pre>
  
  <p>As you can see, the SELECT_W32FUNC macro overrides the globally namespaced PathCombine definition and replaces it with a function reference to either the A or W version of the function.  _S("..") puts the correct string literal in place.</p>
  
  <p>My feeling is that this will be very efficient since the template selector functions will be inlined and have essentially no runtime logic in them, meaning that they should be optimized down to almost nothing, is this a correct assumption?  </p>