title: Typesafe variadic function
body: <p>I want to write a function that accepts a variable number of string literals.  If I was writing in C, I would have to write something like:</p>
  
  <pre><code>void foo(const char *first, ...);
  </code></pre>
  
  <p>and then the call would look like:</p>
  
  <pre><code>foo( "hello", "world", (const char*)NULL );
  </code></pre>
  
  <p>It feels like it ought to be possible to do better in C++.  The best I have come up with is:</p>
  
  <pre><code>template &lt;typename... Args&gt;
  void foo(const char* first, Args... args) {
      foo(first);
      foo(args);
  }
  
  void foo(const char* first) { /* Do actual work */ }
  </code></pre>
  
  <p>Called as:</p>
  
  <pre><code>foo("hello", "world");
  </code></pre>
  
  <p>But I fear that the recursive nature, and the fact that we don't do any type checking until we get to a single argument, is going to make errors confusing if somebody calls <code>foo("bad", "argument", "next", 42)</code>.  What I <em>want</em> to write, is something like:</p>
  
  <pre><code>void foo(const char* args...) {
      for (const char* arg : args) {
          // Real work
      }
  }
  </code></pre>
  
  <p>Any suggestions?</p>
  
  <p>Edit: There is also the option of <code>void fn(std::initializer_list&lt;const char *&gt; args)</code>, but that makes the call be <code>foo({"hello", "world"});</code> which I want to avoid.</p>