GithubHelp home page GithubHelp logo

enum save/load split about cereal HOT 5 CLOSED

uscilab avatar uscilab commented on July 17, 2024
enum save/load split

from cereal.

Comments (5)

DrAWolf avatar DrAWolf commented on July 17, 2024 2

The use case is just serializing enums as strings. A common practice, I think. I do it every day.
You should definitely support that.

from cereal.

AzothAmmo avatar AzothAmmo commented on July 17, 2024

You are going to get errors because we made enum serialization a built in type supported by cereal, found in types/common.hpp, which is included by every archive. What is your use case where you want a custom serialization function for an enum? The only way to remove this issue would be for us to require people to include a header file (e.g. types/enum.hpp) whenever they serialize an enum.

If there is a clean way to get more information into static_assert messages, we'll look into it.

from cereal.

AzothAmmo avatar AzothAmmo commented on July 17, 2024

Just re-visted this and found a simple solution already in cereal. The reason you ran into this issue was that both our generic serialize function for enums (which is currently always pulled in) and your load/save pair were being seen as valid by the compiler, which cereal doesn't like. The way around this is to tell cereal to explicitly use a specific type of serialization function:

namespace cereal
{
  template <class Archive> struct specialize<Archive, MyEnum, cereal::specialization::non_member_load_save> {};
}

see access.hpp for more information on this.

I have a version where enum.hpp is its own header and I'm debating on whether to break it out or not, since with the above essentially one liner you can disambiguate for any type you want.

from cereal.

AzothAmmo avatar AzothAmmo commented on July 17, 2024

Also I'm going to add two macros: CEREAL_SPECIALIZE_FOR_ALL_ARCHIVES and CEREAL_SPECIALIZE_FOR_ARCHIVE that do what I typed out above but in a slightly less verbose looking fashion.

from cereal.

Devacor avatar Devacor commented on July 17, 2024

Minor typo in the comment: Convenienct

Just pointing this out while it's fresh.

from cereal.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.