Created
October 10, 2017 20:42
-
-
Save mtao/8ab481cc8af6688a3d352fb4f0cf7b73 to your computer and use it in GitHub Desktop.
test case to have little containers with extra arguments in some cases
This file contains hidden or 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 <cstddef> | |
| #include <utility> | |
| #include <iostream> | |
| template <typename Obj> | |
| using optional_data_type = decltype(std::declval<Obj>().optional()); | |
| template <typename Obj> | |
| struct Serializer { | |
| public: | |
| Serializer(const Obj& obj): m_id(obj.id()), m_optional(obj.optional()) { | |
| } | |
| private: | |
| int m_id; | |
| optional_data_type<Obj> m_optional; | |
| }; | |
| template <typename Obj> | |
| auto make_serializer(const Obj& o) { return Serializer<Obj>(o); } | |
| class A{ | |
| public: | |
| int id() const { return m_id; } | |
| auto optional() const {return nullptr;} | |
| auto serialize() const { return make_serializer(*this); } | |
| private: | |
| int m_id = 0; | |
| }; | |
| class B{ | |
| public: | |
| struct optional_data { | |
| public: | |
| optional_data(int i) { std::cout << "Optional data: " << i << std::endl; } | |
| }; | |
| int id() const { return m_id; } | |
| auto optional() const {return optional_data(4);} | |
| auto serialize() const { return make_serializer(*this); } | |
| private: | |
| int m_id = 1; | |
| }; | |
| int main() { | |
| A a; | |
| B b; | |
| std::cout << "A serialize: " << std::endl; | |
| auto as = a.serialize(); | |
| std::cout << "B serialize: " << std::endl; | |
| auto bs = b.serialize(); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment