title: Universal solution to 'ISO C99 requires at least one argument for the “…” in a variadic macro'
body: <p>What is the universal way of resolving</p>
  
  <blockquote>
    <p>warning: ISO C++11 requires at least one argument for the "..." in a variadic macro</p>
  </blockquote>
  
  <p>warning introduced by <code>-pedantic</code> flag for gcc?</p>
  
  <p>Consider following example</p>
  
  <pre><code>#define FUNCTION_DECL(ret, name, ...) ret name(__VA_ARGS__)
  
  FUNCTION_DECL(int, foo, int bar, int quax)
  {
      return bar + quax;
  }
  
  FUNCTION_DECL(void, no_args)
  {
  }
  </code></pre>
  
  <p>Expansion of <code>no_args</code> macro <a href="http://coliru.stacked-crooked.com/a/166b8239ba6f6774" rel="nofollow noreferrer">produces</a> error above.</p>
  
  <p>Example of solution to that would be to have two macros and then simple <a href="https://stackoverflow.com/questions/11761703/overloading-macro-on-number-of-arguments">overload trick</a></p>
  
  <pre><code>#define GET_MACRO(_1, _2, _3, _4, _5, _6, _7, _8, _9, NAME, ...) NAME
  #define METHOD_DECL2(ret, name) ret name()
  #define METHOD_DECL3(ret, name, ...) ret name(__VA_ARGS__)
  #define METHOD_DECL(...) GET_MACRO(__VA_ARGS__, METHOD_DECL3, METHOD_DECL3, METHOD_DECL3, METHOD_DECL3, METHOD_DECL3, METHOD_DECL3, METHOD_DECL3, METHOD_DECL2)(__VA_ARGS__)
  </code></pre>
  
  <p>Then <code>METHOD_DECL(void, no_args)</code> no longer triggers an error.</p>
  
  <p>Sadly, it doesn't work in MSVC2017, but is fixed in latest preview of <a href="https://developercommunity.visualstudio.com/content/problem/167330/-va-args-does-not-work-with-multiple-macros.html" rel="nofollow noreferrer">MSVC2019</a>.</p>
  
  <p>And this solution is somewhat clumsy. I tried to think about different solution, something like</p>
  
  <pre><code>#define EXTRACT_FIRST(_1, ...) _1
  #define OMIT_FIRST(_1, ...) __VA_ARGS__
  #define METHOD_DECL(ret, ...) ret EXTRACT_FIRST(__VA_ARGS__) OMIT_FIRST(__VA_ARGS__)
  </code></pre>
  
  <p>But this just moved the problem, since now <code>OMIT_FIRST</code> triggers this error</p>
  
  <p><strong>EDIT:</strong><br>
  What I am trying to do is following:</p>
  
  <p>I have function in AVR assembler (micro-controler)</p>
  
  <pre><code>delay_37us:
      ldi r16, 227
  delay_592c_aw:
      dec r16
      brne delay_592c_aw
      nop
      nop
      ret
  </code></pre>
  
  <p>That active waits for 592cycles (which is 37us for 16MHz cpu)</p>
  
  <p>In order to call this function in C, I have to mark it as <code>.global</code> and also provide C declaration of such function. In order to simplify declaration of such function I would use a macro like that</p>
  
  <pre><code>FUNCTION_DECL(void, delay_37us);
  </code></pre>
  
  <p>which would be defined as</p>
  
  <pre><code>#ifdef __ASSEMBLER__
    #define FUNCTION_DECL(ret, name, ...) .global name
  #else
    #define FUNCTION_DECL(ret, name, ...) ret name(__VA_ARGS__)
  #endif
  </code></pre>
  
  <p>So that I could use one header file for both C and ASM. What I am using currently is one header file with following content</p>
  
  <pre><code>#ifdef __ASSEMBLER__
    .global delay_37us
    .global delay_ms
    .global delay_1s
  #else
    void delay_37us();
    void delay_ms(unsigned char);
    void delay_1s();
  #endif
  </code></pre>
  
  <p>Which is error-prone to maintain.</p>