Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there a way to register polymorphic types during program execution? #819

Open
miki151 opened this issue Mar 15, 2024 · 3 comments
Open

Comments

@miki151
Copy link

miki151 commented Mar 15, 2024

I have a large codebase with a certain class hierarchy where a header contains the base class, and the source file contains a number of subclasses inside a namespace {} block.

I do CEREAL_REGISTER_TYPE for all these subclasses in the source file, because they aren't visible outside of it. For some reason, when compiling my application with mingw, but only on some of my computers, it throws the unregistered polymorphic type exception when serializing.

As far as I can tell, I do everything correctly according to the docs, so I suspect this may be a compiler bug. I tried
writing a minimal program that crashes to isolate the bug without success. I could try to work around it by declaring all the subclasses in the header, but it would be cumbersome.

I'm wondering if it's possible to register my subclasses inside a function in the source file, for example:

void registerTypes() {
  registerType<Subclass1, BinaryOutputArchive>();
  registerType<Subclass2, BinaryOutputArchive>();
// ...
}

And then call registerTypes() at the start of the program.

I'm pretty sure that boost serialization offered such an option. I've been digging through the cereal source code looking for code that I can call to register my types, but couldn't figure it out.

Is there anything else I could try to solve this issue?

@xiaodaxia-2008
Copy link

Also want something like this

@miki151
Copy link
Author

miki151 commented Apr 17, 2024

I have found that this works:
cereal::detail::OutputBindingCreator<OutputArchiveType, SubclassType> tmp;

The constructor will expect cereal::detail::binding_name<SubclassType> to be specialized, you can do it by copying the first part of the CEREAL_REGISTER_TYPE macro or by modifying OutputBindingCreator to take the name in the constructor.

I think this would be a useful feature to add to cereal if any updates are considered.

@redchairman
Copy link

redchairman commented Apr 17, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants