boost::serialize, test if object is serializeable
Asked Answered
B

0

6

I am attempting to write a function that uses boost::serialize if an object can be serialized, with a fallback if the object cannot be written with boost::serialize. Is there some template-based way to test whether an a given class can be serialized?

 // ideally, something of this form
 is_boost_serializeable<T>::value

I know that I can use SFINAE to check for the existence of individual methods, but with the many different options on how to provide a serialization, I'm worried that I would miss some edge cases. As it is, I haven't been able to find anything in the boost documentation describing such a function existing already.

Bergmans answered 4/9, 2016 at 20:46 Comment(4)
Have a look at this: #34587124 That OP wanted to dump/serialize an object/class (i.e. similar to what you're looking for) When I was researching it, my best guess that friending the class would be required. boorg's answer may work but I didn't test it. In other words, each serializable class has to cooperate by providing a common method name. Otherwise, how do you know if there are three ways to serialize: Tom, Dick, or Harry.Racing
How would that work, practically? You can't mix boost archives with non-archive data anywaysJointless
@CraigEstey This is true, that each class must provide some method in order to serialize this. Reading through the boost::serialization documentation, I can see how to create that common method name. What I am uncertain on is not how to create my own class capable of being serialized with boost, but rather how to test whether some template parameter T is capable of being serialized.Bergmans
@Jointless This is for the packing/unpacking of a message passed through TCP after it has been split into a std::vector<char>. One way to pack/unpack the data would be by creating a boost::archive::text_iarchive/test_oarchive. I have successfully done this before, but now want to test whether or not it is possible for a user-defined class. That way, if it is not possible, I can potentially fall back to other methods.Bergmans

© 2022 - 2024 — McMap. All rights reserved.