Question

Thank you very much for your time I really appreciate it

There is a templatized subclass that needs to be serialized with the Cereal Serialization Library, the baseclass is empty, it only exists so we can have a vector of shared_ptr to the base class allowing it to hold multiple types of the templatized subclass, effectively allowing vector storage of multiple variable types.

class NetVar_ {};
template <class VARTYPE> class NetVar : public NetVar_
{
public:
    NetVar(VARTYPE Value)
    {
        Var = Value;
    }

    template <class Archive> void serialize(Archive & archive)
    {
        archive(Var);
    }

private:
    VARTYPE Var;
};

The following vector of the baseclass is pushed a few of the subclasses:

std::vector<std::shared_ptr<NetVar_>> PacketData;
PacketData.push_back(std::make_shared<NetVar<int>>(32));
PacketData.push_back(std::make_shared<NetVar<int>>(32));
PacketData.push_back(std::make_shared<NetVar<std::string>>('test'));

Finally, the vector is serialized and sent off to a remote machine for processing:

std::ostringstream SData;
{
    cereal::PortableBinaryOutputArchive Archive(SData);
    Archive(PacketData);
    //SData is sent to remote machine here through networking library.
}

I must be missing a key piece to the puzzle because when I deserialize the data the program throws an exception, if I debug the values of the output variables are either blank or large negative numbers which leads me to believe the baseclass and or subclass is not getting serialized properly.

The code has been simplified down to only expose the issue, for more information about the complete idea you can refer to this question here.

The following Cereal headers are being included:

#include <cereal\archives\portable_binary.hpp>
#include <cereal\types\vector.hpp>
#include <cereal\types\memory.hpp>
#include <cereal\types\string.hpp>

I'm sure I'll need more as I start to add more types of data into the baseclass.

If someone has any idea what's going on here I would greatly appreciate it.

Thank you again for your time.

Was it helpful?

Solution

You do not have the choice here, polymorphism need virtuality when you have only access to a base class interface. it prevents also Archive to be a template type.

I imagine cereal is doing some SFINAE to test the existence of the serialize method and have a default behavior if not found. That would be the case here as you do not have compilation error.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top