-
Notifications
You must be signed in to change notification settings - Fork 60
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
Custom serialization functions implemented in .cpp files don't work #145
Comments
Try to follow the example in the readme -
|
I tried it like this:
For if I declare them in the header and implement them in a cpp file, these two implementations won't be called both.
Here is how I call them:
well, if I set foo to a const variable, the |
I don’t think you can call a constexpr function across translation units (cpp files), it behaves like an inline function so it has to be defined for each translation unit by which it is used. |
Well, I do find the sulution that is suitable to me. I'll show the code first:
The problem is that, all the template functions or class should be defined in headers, or we need to explicitly instantialize them in .cpp files. The signature of the serialize function is But I don't know the specific type of In this way, I just need to call There are
|
You can always define in cpp instead of header but then zpp::bits won’t be able to serialize you from outside your own cpp file. The way you write the parse and dumps functions seems to suggest you are not interesting in directly serializing your class with zpp::bits from outside your cpp file anyway. No? |
In fact, I have these classes with multiple serialization methods (saving to different standard file types, such as midi and music xml) which I want to encapsulate into a unified interface without exposing the internal implementation details. |
You can still do that while encapsulating zpp::bits by exposing a to_byes from_byes kind of interface from your classes and use zpp::bits internally - but then you lose the ability that zpp::bits provides that it inlines everything and does so recursively. I still hardly see the below code as non-encapsulated however -
this code is not revealing any implementation details while placed in header - the only thing that is revealed here is that you can use a serialize function and that it calls down into an unknown archive (even the type of the archive and return value are not specified) and you don’t even need to include the header file zpp_bits in your header. It is as much exposing implementation details as having the data members listed there. |
I want to serialize a struct without adding a
serialize
function in it.So I just choose to define a
serialize
function outside of it.I test the following case on windows, with vs2022 and g++ in mingw.
If I define the function totally in a header file, it will work, like this:
However, if the function is just declared in the header and implemented in a cpp file, it just don't work.
The text was updated successfully, but these errors were encountered: