title: Reducing code repetition in C++ (or x-treme x-macros)
body: <p>I am using x-macros to reduce the amount of repetition and code duplication while implementing a Lua interface for the game <a href="http://bitfighter.org" rel="nofollow">Bitfighter</a>. The following code works fine:</p>
  
  <pre><code>  //                            Fn name     Valid param profiles  Profile count                           
  #  define TELEPORTER_LUA_METHOD_TABLE \
        TELEPORTER_LUA_METHOD_ITEM(addDest,    ARRAYDEF({{ PT,  END }}), 1 ) \
        TELEPORTER_LUA_METHOD_ITEM(delDest,    ARRAYDEF({{ INT, END }}), 1 ) \
        TELEPORTER_LUA_METHOD_ITEM(clearDests, ARRAYDEF({{      END }}), 1 ) \
  
  
  // BLOCK A Start
  const luaL_reg Teleporter::luaMethods[] =
  {
  #  define TELEPORTER_LUA_METHOD_ITEM(name, b, c) { #name, luaW_doMethod&lt;Teleporter, &amp;Teleporter::name &gt; },
        TELEPORTER_LUA_METHOD_TABLE
  #  undef TELEPORTER_LUA_METHOD_ITEM
     { NULL, NULL }
  };
  // BLOCK A End
  
    /* Generates the following:
    const luaL_reg Teleporter::luaMethods[] =
    {
         { "addDest", luaW_doMethod&lt;Teleporter, &amp;Teleporter::addDest &gt; }
         { "delDest", luaW_doMethod&lt;Teleporter, &amp;Teleporter::delDest &gt; }
         { "clearDests", luaW_doMethod&lt;Teleporter, &amp;Teleporter::clearDests &gt; }
         { NULL, NULL }
    };
    */
  
  
  // BLOCK B Start
  const LuaFunctionProfile Teleporter::functionArgs[] =
  {
  #  define TELEPORTER_LUA_METHOD_ITEM(name, profiles, profileCount) { #name, profiles, profileCount },
        TELEPORTER_LUA_METHOD_TABLE
  #  undef TELEPORTER_LUA_METHOD_ITEM
     { NULL, { }, 0 }
  };
  // BLOCK B End
  
  
    /* Generates the following:
    const LuaFunctionProfile Teleporter::functionArgs[] =
    {
       { "addDest",    {{ PT,  END }}, 1 }
       { "delDest",    {{ INT, END }}, 1 }
       { "clearDests", {{      END }}, 1 }
       { NULL, { }, 0 }
    };
    */
  
  #undef TELEPORTER_LUA_METHOD_TABLE
  </code></pre>
  
  <p>So far, so good.</p>
  
  <p>Except that I do essentially the same thing in dozens of classes.  What I'd REALLY like to do is define the method table in each class (which can be called anything), then define two macros that can be called like this:</p>
  
  <pre><code>GENERATE_LUA_METHODS(Teleporter, TELEPORTER_LUA_METHOD_TABLE)
  GENERATE_FUNCTION_PROFILE(Teleporter, TELEPORTER_LUA_METHOD_TABLE)
  </code></pre>
  
  <p>to avoid all the repeated code above in blocks A &amp; B.  The obvious way is to use a nested macro, but that is, unfortunately, illegal.</p>
  
  <p>Is there a better way?</p>
  
  <hr>
  
  <p>SOLUTION</p>
  
  <hr>
  
  <p>When I posted this question, I was pretty sure the answer would be "can't be done."  Instead, I got two approaches, one of which was exactly what I was looking for.  There was also a good discussion of the pitfalls of macros (there are many), with some alternative approaches suggested.  The implementation I developed based on the accepted answer is clean and easy to understand, with the dirty macro stuff conveniently out-of-sight.</p>
  
  <p>In a hidey-hole somewhere:</p>
  
  <pre><code>#define ARRAYDEF(...) __VA_ARGS__   // Don't confuse the preprocessor with array defs
  
  
  ////////////////////////////////////////
  ////////////////////////////////////////
  //
  // Some ugly macro defs that will make our Lua classes sleek and beautiful
  //
  ////////////////////////////////////////
  ////////////////////////////////////////
  //
  // See discussion of this code here:
  // http://stackoverflow.com/questions/11413663/reducing-code-repetition-in-c
  //
  // Start with a definition like the following:
  // #define LUA_METHODS(CLASS, METHOD) \
  //    METHOD(CLASS, addDest,    ARRAYDEF({{ PT,  END }}), 1 ) \
  //    METHOD(CLASS, delDest,    ARRAYDEF({{ INT, END }}), 1 ) \
  //    METHOD(CLASS, clearDests, ARRAYDEF({{      END }}), 1 ) \
  //
  
  #define LUA_METHOD_ITEM(class_, name, b, c) \
    { #name, luaW_doMethod&lt;class_, &amp;class_::name &gt; },
  
  #define GENERATE_LUA_METHODS_TABLE(class_, table_) \
    const luaL_reg class_::luaMethods[] =            \
    {                                                \
      table_(class_, LUA_METHOD_ITEM)                \
      { NULL, NULL }                                 \
    }
  
  // Generates something like the following:
  // const luaL_reg Teleporter::luaMethods[] =
  // {
  //       { "addDest",    luaW_doMethod&lt;Teleporter, &amp;Teleporter::addDest &gt;    }
  //       { "delDest",    luaW_doMethod&lt;Teleporter, &amp;Teleporter::delDest &gt;    }
  //       { "clearDests", luaW_doMethod&lt;Teleporter, &amp;Teleporter::clearDests &gt; }
  //       { NULL, NULL }
  // };
  
  ////////////////////////////////////////
  
  #define LUA_FUNARGS_ITEM(class_, name, profiles, profileCount) \
    { #name, profiles, profileCount },
  
  #define GENERATE_LUA_FUNARGS_TABLE(class_, table_)  \
    const LuaFunctionProfile class_::functionArgs[] = \
    {                                                 \
      table_(class_, LUA_FUNARGS_ITEM)                \
      { NULL, { }, 0 }                                \
    }
  
  // Generates something like the following:
  // const LuaFunctionProfile Teleporter::functionArgs[] =
  // {
  //    { "addDest",    {{ PT,  END }}, 1 }
  //    { "delDest",    {{ INT, END }}, 1 }
  //    { "clearDests", {{      END }}, 1 }
  //    { NULL, { }, 0 }
  // };
  
  ////////////////////////////////////////
  ////////////////////////////////////////
  </code></pre>
  
  <p>In each class file:</p>
  
  <pre><code>//               Fn name     Param profiles       Profile count                           
  #define LUA_METHODS(CLASS, METHOD) \
     METHOD(CLASS, addDest,    ARRAYDEF({{ PT,  END }}), 1 ) \
     METHOD(CLASS, delDest,    ARRAYDEF({{ INT, END }}), 1 ) \
     METHOD(CLASS, clearDests, ARRAYDEF({{      END }}), 1 ) \
  
  GENERATE_LUA_METHODS_TABLE(Teleporter, LUA_METHODS);
  GENERATE_LUA_FUNARGS_TABLE(Teleporter, LUA_METHODS);
  
  #undef LUA_METHODS
  </code></pre>