Boost logo

Boost Users :

Subject: Re: [Boost-users] SERIALIZE: Registering derived templete classes serialized via base ptr
From: Robert Ramey (ramey_at_[hidden])
Date: 2016-02-17 14:01:59

On 2/17/16 4:35 AM, Unger, Jörg wrote:
> I’m trying to serialize a derived class via a base ptr using boost
> serialize. The example to illustrate the problem (without templates) is
> as follows


> When compiling the above code and linking the object files, everything
> works fine. When I combine everything in a library (in this example it
> is essentially only Derived.o) and link the main file with the library,
> I get an error

what kind of library - shared or static?

> what(): unregistered class - derived class not registered or exported
> The problem seems to be that the registration in the linked library is
> not activated. The problem is simplified here, because in our
> implementation, the derived class is essentially a template class that
> does not have any cpp-files. We have just added another otherwise empty
> cpp file with the registration of all possible instantiations of the class.

have you actually instantiated the templates?

Look in the test suite and examples. There are several there which
illustrate similar scenarios and they are tested on a regular basis.

Also, check the documentation on polymorphic archives. You don't need
this now, but it could be useful once you've got things setup.

> There are several possibilities to solve the problem
> ·The macro BOOST_CLASS_EXPORT_IMPLEMENT(Derived) could be moved to the
> main.cpp. This workaround is not my preferred options, since the
> registration has to be done in every main file we want to use the
> library for.
> ·The macro BOOST_CLASS_EXPORT_IMPLEMENT(Derived) could be moved to the
> Base.cpp (the constructor for the base class has to be implemented in
> this file as well). This is another option, but this requires the header
> files of the derived classes to be included in Base.cpp, which is not
> convenient either.
> ·Serialize one object of the derived class before serialization through
> the pointer, but that’s only a workaround. (see the commented line in
> Main.cpp)

I would expect there to be a derived.cpp and base.cpp which would have
BOOST_CLASS_EXPORT_IMPLEMENT(...) in them and also explicitly
instantiate the templates for the types you need and want to place into
the library.

> ·Call the foo method in Main.cpp. Surprisingly, this does not depend on
> whether the foo function is implemented in Derived.cpp, but also if it
> is called in Main.cpp. Surprisingly, if “;” in Main.cpp
> is uncommented the program works fine, which is something I do not
> understand. It seems that the registration is only performed, if one of
> the functions in this cpp file is actually called, or am I wrong?
> ·Implement the constructor of Derived in Derived.cpp. This approach
> works fine for the current example (somehow similar to the previous
> solution). However, in our case, Derived is a template and thus the
> constructor can’t be implemented in the cpp file.
> ·Another option is to always link the main file with the library _and_
> the object file Derived.o, but this is also not a convenient option.
> ·Finally, a manual registration with register_type is possible. This
> could be moved to a separate subroutine that is always called in
> different main files.
> What is the recommended way of registering derived template classes that
> are serialized through a base ptr within a library.

Unfortunately, there is not a no-brainer rule that address the issue.
One has to think about explicit instantiations, suppressing dead-code
elimination in specific cases and the like. That is it doesn't just
work - it has to be made to work.

Good news is that once you've got it set up it works very well.

Try checking out the library tests and examples and building and running
the tests and examples. This will give the magic information you need.

Robert Ramey
> Best regards,
> Jörg F. Unger
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]

Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at