c++ - Create hana tuple with unmovable / noncopyable types -
i'm trying wrap head around great boost::hana , try figure out how translate old school tuple code.
maybe it's bit special have types neither copyable nor moveable , want pack them in container. think policy design ...
i guess i'm not getting how create hana tuple in value world. way i'm trying doesn't work because make<tuple>(compt{}...)
needs types either copyable or @ least moveable.
so not way it. or limitation of hana?
thanks!
struct nonmoveable { nonmoveable() = default; nonmoveable(const nonmoveable& other) = delete; int val; }; struct simple { int val; }; template <typename ...compt> struct oldschoolcontainer { template <int i> auto& getcomponent() { return std::get<i>(components); } std::tuple<compt...> components; }; template <typename ...compt> struct hanacontainer { using args_t = decltype(make<tuple>(compt{}...)); template <int i> auto& getcomponent() { return components[int_<i>]; } args_t components; }; int main() { oldschoolcontainer<simple> simpleosc; static_assert(std::is_same<simple&, decltype(simpleosc.getcomponent<0>())>::value, ""); hanacontainer<simple> simplehanac; static_assert(std::is_same<simple&, decltype(simplehanac.getcomponent<0>())>::value, ""); oldschoolcontainer<nonmoveable> nonmoveableosc; static_assert(std::is_same<nonmoveable&, decltype(nonmoveableosc.getcomponent<0>())>::value, ""); // !!! not compile because hana's tuple closure needs either move or copy hanacontainer<nonmoveable> nonmoveablehanac; static_assert(std::is_same<nonmoveable&, decltype(nonmoveablehanac.getcomponent<0>())>::value, ""); return 0; }
you can use hana::_tuple<...>
type std::tuple
. hence, following works:
#include <boost/hana/integral_constant.hpp> #include <boost/hana/tuple.hpp> #include <tuple> #include <type_traits> namespace hana = boost::hana; struct nonmoveable { nonmoveable() = default; nonmoveable(const nonmoveable& other) = delete; int val; }; struct simple { int val; }; template <typename ...compt> struct hanacontainer { template <int i> auto& getcomponent() { return components[hana::int_<i>]; } hana::_tuple<compt...> components; }; int main() { hanacontainer<simple> simplehanac; static_assert(std::is_same<simple&, decltype(simplehanac.getcomponent<0>())>::value, ""); hanacontainer<nonmoveable> nonmoveablehanac; static_assert(std::is_same<nonmoveable&, decltype(nonmoveablehanac.getcomponent<0>())>::value, ""); return 0; }
Comments
Post a Comment