I have "solved" a similar problem in production code. First, I have an ordinary struct (actually a class with various member functions, but it's only the data members which we are interested in here)...
class Record
{
std::string name;
int age;
std::string email;
MYLIB_ENABLE_TUPLE(Record) // macro
};
Then just below the struct definition, but outside of any namespace, I have another macro:
MYLIB_DECLARE_TUPLE(Record, (o.name, o.age, o.email))
The disadvantage with this approach is that the member names must be listed twice, but this is the best I have been able to come up with while still permitting traditional member access syntax within the struct's own member functions. The macro appears very near the definitions of the data members themselves, so it is not too hard to keep them in sync with each other.
In another header file I have a class template:
template <class T>
class TupleConverter;
The first macro is defined so as to declare this template to be a friend
of the struct, so it can access its private data members:
#define MYLIB_ENABLE_TUPLE(TYPE) friend class TupleConverter<TYPE>;
The second macro is defined so as to introduce a specialization of the template:
#define MYLIB_DECLARE_TUPLE(TYPE, MEMBERS) \
template <> \
class TupleConverter<TYPE> \
{ \
friend class TYPE; \
static auto toTuple(TYPE& o) \
-> decltype(std::tie MEMBERS) \
{ \
return std::tie MEMBERS; \
} \
public: \
static auto toTuple(TYPE const& o) \
-> decltype(std::tie MEMBERS) \
{ \
return std::tie MEMBERS; \
} \
};
This creates two overloads of the same member function name, TupleConverter<Record>::toTuple(Record const&)
which is public, and TupleConverter<Record>::toTuple(Record&)
which is private and accessible only to Record
itself through friendship. Both return their argument converted to a tuple of references to private data members by way of std::tie
. The public const overload returns a tuple of references to const, the private non-const overload returns a tuple of references to non-const.
After preprocessor substitution, both friend
declarations refer to entities defined in the same header file, so there should be no chance of other code abusing the friendship to break encapsulation.
toTuple
can't be a member function of Record
, because its return type can't be deduced until the definition of Record
is complete.
Typical usage looks like this:
// lexicographical comparison
bool operator< (Record const& a, Record const& b)
{
return TupleConverter<Record>::toTuple(a) < TupleConverter<Record>::toTuple(b);
}
// serialization
std::ostream& operator<< (std::ostream& os, Record const& r)
{
// requires template<class... Ts> ostream& operator<<(ostream&, tuple<Ts...>) defined elsewhere
return os << TupleConverter<Record>::toTuple(r);
}
There are many ways this could be extended, for example by adding another member function in TupleConverter
which returns a std::vector<std::string>
of the names of the data members.
If I'd been allowed to use variadic macros then the solution might have been even better.
enum
?struct
?tuple
functionality then I'd say usestruct {std::string name, int age, std::string email} get_record();
. You could name the return type, but in C++11 it's not really necessary since it can be captured withauto
ordecltype
.struct
at compile-time using template metaprogramming. Sincestd::pair
andstd::tuple
already allow this, it must be possible to design the language do this for ordinarystruct
s too, and if that were the case then we wouldn't need specialtuple
types in the first place.