I have a multiple classes each with different member variables that are initialized trivially in a constructor. Here is an example:

struct Person { Person(const char *name, int age) : name(name), age(age) { } private: const char *name; int age; };

Each has an associated print<>() function.

template <> void print<Person>(const Person &person) { std::cout << "name=" << name << "

"; std::cout << "age=" << age << "

"; }

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.

For example, could I use the X-args preprocessor technique -- something like this?

#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 <> void print<Person>(const Person &person) { LIST_COUT_LINE(ARGUMENTS) } #undef ARGUMENTS

Or better, a template-based approach?

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.