title: How can I retrieve the last argument of a C99 variadic macro?
body: <p>Visual Studio's error message for a failed static_assert consists entirely of just an error code and the second parameter to the static_assert, without any additional message indicating that it was a static assertion failure.  I'd like to make a macro to solve this problem.  For example, as a first try:</p>
  
  
  
  <pre class="lang-cpp prettyprint-override"><code>#define STATIC_ASSERT(x) static_assert(x, "static assertion failed: " #x)
  </code></pre>
  
  <p>The first issue that you run into is that the C preprocessor doesn't understand <code>&lt; &gt;</code> as being enclosing delimiters, which results in syntax errors with templates.  The following becomes illegal:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>template &lt;typename T, typename U&gt;
  auto SafeMultiply(T x, U y) -&gt; decltype(x * y)
  {
      STATIC_ASSERT(std::is_same&lt;T, U&gt;::value);
      STATIC_ASSERT(!std::numeric_limits&lt;T&gt;::is_signed);
      if (x &gt; (std::numeric_limits&lt;decltype(x * y)&gt;::max)())
          throw std::overflow_error("multiplication overflow");
      return x * y;
  }
  </code></pre>
  
  <p>This is illegal because the comma between T and U in the first <code>STATIC_ASSERT</code> is interpreted as separating two macro parameters, rather than template parameters.  The C preprocessor throws an error because the <code>STATIC_ASSERT</code> macro only takes one parameter.</p>
  
  <p>The two main solutions to this problem are to use double parentheses and, more recently, to use variadic macros:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>// Invoke the macro this way...
  STATIC_ASSERT((std::is_same&lt;T, U&gt;::value));
  // ...or define it this way:
  #define STATIC_ASSERT(...) static_assert((__VA_ARGS__), \
      "static assertion failed: " #__VA_ARGS__)
  </code></pre>
  
  <p>The latter solution is better, requiring only changes to the macro definition.  (The extra parentheses around <code>__VA_ARGS__</code> in the new definition are to preserve proper order of operations in some of the weirder cases.  It may not matter in this particular macro, but it's a good habit to put parentheses around your macros' parameters in your macro definitions.)</p>
  
  <p>Now what if I wanted to change my <code>STATIC_ASSERT</code> macro to take a message like the standard C++ <code>static_assert</code>, but adding a prefix to the message?  Kind of like this, but supporting the use of <code>std::is_same&lt;T, U&gt;</code> without the use of double parentheses:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>// Causes a syntax error :(
  #define STATIC_ASSERT(expr, msg) static_assert((expr), \
      "static assertion failed: " msg)
  STATIC_ASSERT(std::is_same&lt;T, U&gt;, "x and y are not of the same type");
  </code></pre>
  
  <p>If I could get the last parameter of a variadic macro, it would work:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>// I wish this'd work
  #define STATIC_ASSERT(..., msg) static_assert((__VA_ARGS__), \
      "static assertion failed: " msg)
  STATIC_ASSERT(std::is_same&lt;T, U&gt;, "x and y are not of the same type");
  </code></pre>
  
  <p>But since that's not legal, how can I legally get the last parameter of a <code>...</code> macro parameter set?  Sure, I could reverse the parameter order, but then it's not the same as <code>static_assert</code>.</p>