title: C++ preprocessor: avoid code repetition of member variable list
body: <p>I have a multiple classes each with different member variables that are initialized trivially in a constructor.  Here is an example:</p>
  
  <pre><code>struct Person
  {
      Person(const char *name, int age)
          :
          name(name),
          age(age)
      {
      }
  private:
      const char *name;
      int age;
  };
  </code></pre>
  
  <p>Each has an associated <code>print&lt;&gt;()</code> function.</p>
  
  <pre><code>template &lt;&gt;
  void print&lt;Person&gt;(const Person &amp;person)
  {
      std::cout &lt;&lt; "name=" &lt;&lt; name &lt;&lt; "\n";
      std::cout &lt;&lt; "age=" &lt;&lt; age &lt;&lt; "\n";
  }
  </code></pre>
  
  <p>This code is error prone since the parameter list is replicated in four places.  How can I rewrite the code to avoid this duplication?  I'd like to use the preprocessor and/or templates.</p>
  
  <p>For example, could I use the X-args preprocessor technique -- something like this?</p>
  
  <pre><code>#define ARGUMENTS \
      ARG(const char *, name) \
      ARG(int, age)
  
  struct Person
  {
      Person(LIST_TYPE_NAME_COMMA(ARGUMENTS))
         :
         LIST_NAME_INIT(ARGUMENTS)
      {
      }
  private:
      LIST_TYPE_NAME_SEMICOLON(ARGUMENTS)
  };
  
  template &lt;&gt;
  void print&lt;Person&gt;(const Person &amp;person)
  {
     LIST_COUT_LINE(ARGUMENTS)
  }
  
  #undef ARGUMENTS
  </code></pre>
  
  <p>Or better, a template-based approach?</p>
  
  <p>Please don't question why I want to do this, there are reasoned design decisions that have resulted in multiple similar objects with named parameters.  The parameters need to be named member variables for performance reasons.  I'm just exploring whether it's possible to list the parameters and their types only once.</p>