Last active
October 17, 2015 10:28
-
-
Save skyrpex/a5d2c99e82cbbdf8d078 to your computer and use it in GitHub Desktop.
Revisions
-
skyrpex revised this gist
Oct 17, 2015 . 1 changed file with 6 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -34,6 +34,12 @@ class Container { optional<C>(index).emplace(args...); } template<class C> bool has(std::size_t index) const { return optional<C>(index); } protected: template<class C> -
skyrpex revised this gist
Oct 17, 2015 . 1 changed file with 10 additions and 10 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -8,8 +8,8 @@ class Container { public: template<class T> using Optional = std::experimental::optional<T>; std::vector<std::tuple<Optional<Cs>...>> elements; template<class C> C get(std::size_t index) const @@ -24,35 +24,35 @@ class Container } template<class C> void set(std::size_t index, const C& c) { optional<C>(index) = c; } template<class C, class... Args> void emplace(std::size_t index, Args&&... args) { optional<C>(index).emplace(args...); } protected: template<class C> const Optional<C>& optional(std::size_t index) const { return std::get<Optional<C>>(elements.at(index)); } template<class C> Optional<C>& optional(std::size_t index) { return std::get<Optional<C>>(elements.at(index)); } }; int main() { Container<int, double> container; container.elements.resize(10); container.emplace<int>(0, 69); std::cout << container.get<int>(0) << std::endl; return 0; -
skyrpex created this gist
Oct 17, 2015 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,59 @@ #include <iostream> #include <tuple> #include <vector> #include <experimental/optional> template<class ...Cs> class Container { public: template<class T> using Optional = std::experimental::optional<T>; std::vector<std::tuple<Optional<Cs>...>> elements_; template<class C> C get(std::size_t index) const { return optional<C>(index).value(); } template<class C> C get(std::size_t index, C&& defaultValue) const { return optional<C>(index).value_or(std::move(defaultValue)); } template<class C> void set(std::size_t index, const C &c) { optional<C>(index) = c; } template<class C, class ...Args> void emplace(std::size_t index, Args &&...args) { optional<C>(index).emplace(args...); } protected: template<class C> const Optional<C> &optional(std::size_t index) const { return std::get<Optional<C>>(elements_.at(index)); } template<class C> Optional<C> &optional(std::size_t index) { return std::get<Optional<C>>(elements_.at(index)); } }; int main() { Container<int, double> container; container.elements_.resize(10); container.emplace<int>(0, 69); std::cout << container.get<int>(0) << std::endl; return 0; }