cereal icon indicating copy to clipboard operation
cereal copied to clipboard

specialise does not seem to work with enums?

Open MichaelVoelkel opened this issue 4 years ago • 0 comments

I have read the question regarding this before: https://github.com/USCiLab/cereal/issues/25.

I'm trying to accomplish the same: serialise enums via string, in my case with magic_enum lib. However, I get on macOS the error that save_minimal is ambiguous. I thought, the macro would solve this but does not seem like it:

struct Hello
{
    enum class Test {abc, def};
    
    Test t;
};

namespace cereal
{
// Enabled for text archives (e.g. XML, JSON)
template <class Archive,
        cereal::traits::EnableIf<cereal::traits::is_text_archive<Archive>::value>
        = cereal::traits::sfinae>
std::string save_minimal( Archive &, const Hello::Test& h )
{
    return std::string(magic_enum::enum_name(h));
}

// Enabled for text archives (e.g. XML, JSON)
template <class Archive,
        cereal::traits::EnableIf<cereal::traits::is_text_archive<Archive>::value>
        = cereal::traits::sfinae>
void load_minimal( Archive const &, std::string const & str, Hello::Test& hello )
{
    hello = magic_enum::enum_cast<Hello::Test>(str).value();
}
}

CEREAL_SPECIALIZE_FOR_ALL_ARCHIVES( Hello::Test, cereal::specialization::non_member_load_save_minimal );

// later in the same file

BOOST_AUTO_TEST_CASE(cerealTest)
{
    std::cout << "cerealTest" << "..." << std::endl;
    
    cereal::JSONOutputArchive ar( std::cout );
    
    Hello h { Hello::Test::abc};
    Hello::Test x {Hello::Test::abc};
    
    ar(x);
}

And yeah, "call to save_minimal" is ambiguous. My version and the one from common.hpp, starting from line 91 with //! Saving for enum types comment beforehand are candidates... Any ideas what I could be doing wrong?

PS: magic_enum does not really matter here, I could just return a dummy string or a dummy enum value in save/load functions.

Edit: Hmmm, when I just uncomment the version from cereal, then my version does also not work... not sure, if specialising with external save_minimal works like I tried, probably not? The documentation only shows the internal version.

Edit2: When removing the common.hpp code and move my specialisations into global instead of cereal namespace, then it works... but probably not intended.

MichaelVoelkel avatar Oct 16 '21 11:10 MichaelVoelkel