C++ Cereal Archive Type Specialization not working

601 Views Asked by At

I'm using Cereal 1.1.2 with VS 2013.

I've tried the example for archive specialization with types from here: http://uscilab.github.io/cereal/archive_specialization.html

But it doesn't compile with the error:

error C2665: 'cereal::make_nvp' : none of the 3 overloads could convert all the argument types ... while trying to match the argument list '(const std::string, const std::string)

I'm trying to compile the following code, using the snippets from the example:

#include "cereal\types\map.hpp"

namespace cereal
{
    //! Saving for std::map<std::string, std::string> for text based archives
    // Note that this shows off some internal cereal traits such as EnableIf,
    // which will only allow this template to be instantiated if its predicates
    // are true
    template <class Archive, class C, class A,
        traits::EnableIf<traits::is_text_archive<Archive>::value> = traits::sfinae> inline
    void save(Archive & ar, std::map<std::string, std::string, C, A> const & map)
    {
        for (const auto & i : map)
            ar(cereal::make_nvp<Archive>(i.first, i.second));
    }

    //! Loading for std::map<std::string, std::string> for text based archives
    template <class Archive, class C, class A,
        traits::EnableIf<traits::is_text_archive<Archive>::value> = traits::sfinae> inline
    void load(Archive & ar, std::map<std::string, std::string, C, A> & map)
    {
        map.clear();

        auto hint = map.begin();
        while (true)
        {
            const auto namePtr = ar.getNodeName();

            if (!namePtr)
                break;

            std::string key = namePtr;
            std::string value; ar(value);
            hint = map.emplace_hint(hint, std::move(key), std::move(value));
        }
    }
} // namespace cereal


#include "cereal\archives\json.hpp"
#include <iostream>
#include <string>
#include <fstream>

int main()
{
    std::stringstream ss;
    {
        cereal::JSONOutputArchive ar(ss);
        std::map<std::string, std::string> filter = { { "type", "sensor" }, { "status", "critical" } };

        ar(CEREAL_NVP(filter));
    }

    std::cout << ss.str() << std::endl;

    {
        cereal::JSONInputArchive ar(ss);
        cereal::JSONOutputArchive ar2(std::cout);

        std::map<std::string, std::string> filter;

        ar(CEREAL_NVP(filter));
        ar2(CEREAL_NVP(filter));
    }

    std::cout << std::endl;
    return 0;
}

Note that if I remove the save() function overload it does compile. But my goal is to be able to use the map key as the JSON key, so it comes out like this:

{
    "map": {
        "a": 1,
        "b": 2
    },
    "map_not_string": [
        {
            "key": 1,
            "value": 1
        },
        {
            "key": 2,
            "value": 2
        }
    ]
}
1

There are 1 best solutions below

0
On BEST ANSWER

I brought this question as an issue on Cereal's Github and got an answer: https://github.com/USCiLab/cereal/issues/197

The problem was the templated version of make_nvp, so

ar(cereal::make_nvp<Archive>(i.first, i.second));

becomes

ar(cereal::make_nvp(i.first, i.second));

The documentation was updated so this shouldn't be a problem anymore.