Skip to content

Instantly share code, notes, and snippets.

@sithhell
Created July 12, 2013 13:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sithhell/5984469 to your computer and use it in GitHub Desktop.
Save sithhell/5984469 to your computer and use it in GitHub Desktop.
cereal multiple definition example. compile with: CEREAL_INCLUDE=/path/to/cereal/include make
#ifndef AG
#define AG
struct A
{
virtual ~A() {}
virtual void hello()=0;
};
#endif
#include "b.hpp"
#include <iostream>
// FIXME: this is needed after the patch in only one TU
// CEREAL_BIND_TO_ARCHIVES(B)
B::B() {}
void B::hello()
{
std::cout << "B\n";
}
#ifndef BG
#define BG
#include <cereal/archives/binary.hpp>
#include <cereal/types/polymorphic.hpp>
#include "a.hpp"
struct B : A
{
B();
void hello();
template <typename Archive>
void serialize(Archive & ar)
{
}
};
CEREAL_REGISTER_TYPE(B)
#endif
#include "c.hpp"
#include <iostream>
// FIXME: this is needed after the patch in only one TU
// CEREAL_BIND_TO_ARCHIVES(C)
C::C() {}
void C::hello()
{
std::cout << "C\n";
}
#ifndef CG
#define CG
#include <cereal/archives/binary.hpp>
#include <cereal/types/polymorphic.hpp>
#include "a.hpp"
struct C : A
{
C();
void hello();
template <typename Archive>
void serialize(Archive & ar)
{
}
};
CEREAL_REGISTER_TYPE(C)
#endif
diff --git a/include/cereal/types/polymorphic.hpp b/include/cereal/types/polymorphic.hpp
index 6e966d4..28745df 100644
--- a/include/cereal/types/polymorphic.hpp
+++ b/include/cereal/types/polymorphic.hpp
@@ -62,7 +62,6 @@
static constexpr char const * name() { return #T; }; \
}; \
} } /* end namespaces */ \
- CEREAL_BIND_TO_ARCHIVES(T);
//! Registers a polymorphic type with cereal, giving it a
//! user defined name
@@ -77,7 +76,6 @@
struct binding_name<T> \
{ static constexpr char const * name() { return Name; }; }; \
} } /* end namespaces */ \
- CEREAL_BIND_TO_ARCHIVES(T);
namespace cereal
{
#include "b.hpp"
#include "c.hpp"
#include <memory>
#include <cereal/cereal.hpp>
int main()
{
std::stringstream stream;
{
cereal::BinaryOutputArchive oarchive(stream);
std::shared_ptr<A> b(new B);
std::shared_ptr<A> c(new C);
oarchive(b, c);
}
{
cereal::BinaryInputArchive iarchive(stream);
std::shared_ptr<A> b;
std::shared_ptr<A> c;
iarchive(b, c);
b->hello();
c->hello();
}
}
all: main
b.o: b.cpp b.hpp a.hpp
g++ -std=c++11 -c b.cpp -I$(CEREAL_INCLUDE)
c.o: c.cpp c.hpp a.hpp
g++ -std=c++11 -c c.cpp -I$(CEREAL_INCLUDE)
main: main.cpp b.o c.o
g++ -std=c++11 -o test main.cpp b.o c.o -I$(CEREAL_INCLUDE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment