title: How to write a c++ assert macro with a varying number of informational arguments?
body: <p>I am trying to write a macro <code>dbgassert</code> similar to the standard <code>assert</code>. In addition to what <code>assert</code> does, I want to <code>dbgassert</code> print an arbitrary number of additional parameters (containing debugging information).</p>
  
  <p>What I have so far is listed below, which is adapted from <a href="https://stackoverflow.com/questions/9701229/c-assert-implementation-in-assert-h/9701408#9701408">this SO answer</a>. But I am having issue in my code with either variadic templates or macros. If I use at least one additional argument (the OK line), then <code>dbgassert</code> works as expected. But if I give no additional argument, then compilation fails (the problem line).</p>
  
  <p>I have some experience with variadic template programming (like how to print a tuple), but I haven't used variadic macros before. </p>
  
  <p>Can some please explain what's the proper way of writing this variadic parameters macro combination?</p>
  
  <p>By the way, could someone explain the <code>#EX</code> magic in the macro? It shows the expression and works for me on gcc4.8.1. Is it universally supported?</p>
  
  <p>Thanks,</p>
  
  <hr>
  
  <p>Code:</p>
  
  <pre><code>//corrected reserved identifier issue and assumption issues per comments
  #include &lt;cassert&gt;
  #include &lt;iostream&gt;
  using namespace std;
  
  template &lt;typename ...Args&gt;
  void realdbgassert(const char *msg, const char *file, int line, Args ... args) {
    cout &lt;&lt; "Assertion failed! \nFile " &lt;&lt; file &lt;&lt; ", Line " &lt;&lt; line &lt;&lt; endl 
         &lt;&lt; "  Expression: " &lt;&lt; msg &lt;&lt; endl;
    std::abort();
  }
  
  #define dbgassert(EX,...) \
    (void)((EX) || (realdbgassert (#EX, __FILE__, __LINE__, __VA_ARGS__),0))
  
  int main() {
    dbgassert(1&gt;2,"right","yes"); //OK
    dbgassert(1&gt;2,"right"); //OK.
    //dbgassert(1&gt;2); //Problem. compile error: expected primary-expression before ')' token
                    //#define dbgassert(EX,...) (void)((EX) || (realdbgassert (#EX, __FILE__, __LINE__, __VA_ARGS__)^,0))
  }
  </code></pre>
  
  <hr>
  
  <p>Original version of the code.</p>
  
  <pre><code>#include &lt;cassert&gt;
  #include &lt;sstream&gt;
  using namespace std;
  
  #ifdef __cplusplus
  extern "C" {
  #endif
  extern void __assert (const char *msg, const char *file, int line);
  #ifdef __cplusplus
  };
  #endif
  
  template &lt;typename ...Args&gt;
  void _realdbgassert(const char *msg, const char *file, int line, Args ... args) {
      stringstream os;
      //... do something
      __assert(msg,file,line);
  }
  #define dbgassert(EX,...) (void)((EX) || (_realdbgassert (#EX, __FILE__, __LINE__, __VA_ARGS__),0))
  
  int main() {
    dbgassert(1==0,"right"); //Problem line: undefined reference to `__assert'
  } 
  </code></pre>