title: Conveniently Declaring Compile-Time Strings in C++
body: <p>Being able to create and manipulate strings during compile-time in C++ has several useful applications. Although it is possible to create compile-time strings in C++, the process is very cumbersome, as the string needs to be declared as a variadic sequence of characters, e.g.</p>
  
  <pre><code>using str = sequence&lt;'H', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd', '!'&gt;;
  </code></pre>
  
  <p>Operations such as string concatenation, substring extraction, and many others, can easily be implemented as operations on sequences of characters. <strong>Is it possible to declare compile-time strings more conveniently? If not, is there a proposal in the works that would allow for convenient declaration of compile-time strings?</strong></p>
  
  <h3>Why Existing Approaches Fail</h3>
  
  <p>Ideally, we would like to be able to declare compile-time strings as follows:</p>
  
  <pre><code>// Approach 1
  using str1 = sequence&lt;"Hello, world!"&gt;;
  </code></pre>
  
  <p>or, using user-defined literals,</p>
  
  <pre><code>// Approach 2
  constexpr auto str2 = "Hello, world!"_s;
  </code></pre>
  
  <p>where <code>decltype(str2)</code> would have a <code>constexpr</code> constructor. A messier version of approach 1 is possible to implement, taking advantage of the fact that you can do the following:</p>
  
  <pre><code>template &lt;unsigned Size, const char Array[Size]&gt;
  struct foo;
  </code></pre>
  
  <p>However, the array would need to have external linkage, so to get approach 1 to work, we would have to write something like this:</p>
  
  <pre><code>/* Implementation of array to sequence goes here. */
  
  constexpr const char str[] = "Hello, world!";
  
  int main()
  {
      using s = string&lt;13, str&gt;;
      return 0;
  }
  </code></pre>
  
  <p>Needless to say, this is very inconvenient. Approach 2 is actually not possible to implement. If we were to declare a (<code>constexpr</code>) literal operator, then how would we specify the return type? Since we need the operator to return a variadic sequence of characters, so we would need to use the <code>const char*</code> parameter to specify the return type:</p>
  
  <pre><code>constexpr auto
  operator"" _s(const char* s, size_t n) -&gt; /* Some metafunction using `s` */
  </code></pre>
  
  <p>This results in a compile error, because <code>s</code> is not a <code>constexpr</code>. Trying to work around this by doing the following does not help much.</p>
  
  <pre><code>template &lt;char... Ts&gt;
  constexpr sequence&lt;Ts...&gt; operator"" _s() { return {}; }
  </code></pre>
  
  <p>The standard dictates that this specific literal operator form is reserved for integer and floating-point types. While <code>123_s</code> would work, <code>abc_s</code> would not. What if we ditch user-defined literals altogether, and just use a regular <code>constexpr</code> function?</p>
  
  <pre><code>template &lt;unsigned Size&gt;
  constexpr auto
  string(const char (&amp;array)[Size]) -&gt; /* Some metafunction using `array` */
  </code></pre>
  
  <p>As before, we run into the problem that the array, now a parameter to the <code>constexpr</code> function, is itself no longer a <code>constexpr</code> type.</p>
  
  <p>I believe it should be possible to define a C preprocessor macro that takes a string and the size of the string as arguments, and returns a sequence consisting of the characters in the string (using <code>BOOST_PP_FOR</code>, stringification, array subscripts, and the like). However, I do not have the time (or enough interest) to implement such a macro =)</p>