When using the BOOST_SERIALIZATION_NVP
macro to create a name-value pair for XML serialization, the compiler happily allows the following code to compile, even though the element name is not a valid XML element and an exceptions is thrown when trying to actually serialize the object into XML:
BOOST_SERIALIZATION_NVP(_member[index])
An obvious fix is to use:
boost::serialization::make_nvp("ValidMemberName", _member[index])
But can anyone suggest a way to modify boost so that illegitimate element names would trigger a compilation error? (thus not relying on unit testing to catch the above subtle bug)
Edit:
One idea is to somehow declare a dummy local variable with the name of the element passed to the macro, assuming the set of valid identifiers in C++ is a subset of valid XML elements. Not entire sure this can be done though.