c++ - Why make_nvp needs non-const reference? -
why non-const reference here?
template<class t> const nvp< t > make_nvp(const char * name, t & t);
the reason i'm asking have structure public fields , need make them private , use accessors instead. know if i'm allowed use temporary variable , pass them make_nvp
or need befriend serializer data structure.
// option 1 auto = data.geta(); ar & make_nvp("a", a); // option 2 ar & make_nvp("a", data._a); // _a private, serializer friend
i don't know ar
because it's templated parameter, in cases make use of non-constness , save later use , option 1
problematic.
in boost archive, can use single function both serialization , deserialization. achieved using archive template argument - can output archive serializes structure or input archive loads struct file. deserialization function needs non-const reference store deserialized value , that's why make_nvp
needs non-const reference.
coming question: option 2 makes more sense, because option 1 breaks deserialization.
Comments
Post a Comment