cereal icon indicating copy to clipboard operation
cereal copied to clipboard

CEREAL_REGISTER_POLYMORPHIC_RELATION requires the base class to be abstract

Open johngladp opened this issue 5 years ago • 1 comments

It isn't stated in the documentation regarding polymorphism but it appears that using CEREAL_REGISTER_POLYMORPHIC_RELATION requires that the specified base class is abstract.

If the base class isn't abstract then a static_assert fires regarding "cereal could not find any input serialization functions for the provided type and archive combination." (inside ArchiveType & processImpl(T const &) - cereal.hpp #954).

This causes a lot of confusion, personally anyway, and took me quite a while to identify the class trait that made my code not compile and the examples compile fine!

I suggest two remedies (which I'm happy to put a pull request up for if it is indeed intended behaviour to require the base to be abstract and not otherwise a bug):

1> Document that the base class must be abstract. Also mention the C++ oxymoron of a pure virtual destructor, with it's mandatory implementation, as a means to make the class abstract (i.e. virtual ~session_configuration_base() = 0 { /*impl */ } ) (gotta love C++)

2> Add a static_assert within the CEREAL_REGISTER_POLYMORPHIC_RELATION macro that verifies that the base class is indeed polymorphic. Such as: static_assert(std::is_abstract_v<Base>, "The base class must be abstract");

johngladp avatar May 27 '20 16:05 johngladp

I think this needs to be highlighted in the main page/sample code as well (I would have been stuck for a long time if it wasn't for this post).

ktimam avatar Jun 04 '25 20:06 ktimam