title: C++ Create BSTR at compile time / insert length into string at compile time?
body: <p>Is it possible using macro magic or TMP to insert the length into a string at compile time?</p>
  
  <p>For example:</p>
  
  <pre><code>const wchar_t* myString = L"Hello";
  </code></pre>
  
  <p>I would want the buffer to actually contain "[length] [string constant]".</p>
  
  <p>I'm using MSVC 2010 which lacks constexpr. I figured there must be some trick to make this work as its possible to do:</p>
  
  <pre><code>const wchar_t* myString = L"\x005Hello";
  </code></pre>
  
  <p>My attempt so far:</p>
  
  <pre><code>template&lt;int Size&gt;
  wchar_t* toBstr(const wchar_t* str)
  {
      #pragma pack(push)
      #pragma pack(1)
      struct BStr
      {
         int len;
         wchar_t data[Size];
      };
      #pragma pack(pop)
  
    static BStr ret;
    ret.len = Size;
  
    // don't want to have to copy here, how else could this work??
    //ret.data = str;
  
    return ret.data;
   }
  
   const wchar_t* m = toBstr&lt;_countof(L"Hello")&gt;(L"Hello");
  </code></pre>
  
  <p>This question seems related:</p>
  
  <p><a href="https://stackoverflow.com/questions/4693819/c-template-string-concatenation">C++ template string concatenation</a></p>
  
  <p>But not concat for two string constants, rather a constant generated from the length of the 2nd :)</p>