Created
July 12, 2013 13:30
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef AG | |
#define AG | |
struct A | |
{ | |
virtual ~A() {} | |
virtual void hello()=0; | |
}; | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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"; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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"; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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