I want to define interface for serializing variables, where depending on a template argument, serialization code (true
) or nothing (false
) is performed. The serialization function is itself templated on archive and variable types.
Is it better to partially specialize serialization function, or specialize class with static method inside? Is the code below correct? Is there a better way?
This is my pseudo-code where I am not sure, whether it would work or not.
The class-approach looks something like this:
template<bool ser>
struct MaybeSerialize;
template<> struct MaybeSerialize<true>{
template<class ArchiveT, class T>
static void serialize(ArchiveT& archive, T& var){ /* serialize*/ }
};
template<> struct MaybeSerialize<false>{
template<class ArchiveT, class T>
static void serialize(ArchiveT& archive, T& var){ /* do nothing*/ }
};
// call like this
MaybeSerialize<compileTimeBool>::serialize(archive,variable);
The partial specialization would be (the syntax is probably wrong):
template<bool ser, class Archive T, typename T> maybeSerialize(ArchiveT&,T&);
template<class ArchiveT, typename T> maybeSerialize<true>(ArchiveT&,T&){ /* serialize */ }
template<class ArchiveT, typename T> maybeSerialize<false>(ArchiveT&,T&){ /* do nothing */ }
// call like this
maybeSerialize<compileTimeBool>(archive,variable);