Created
March 19, 2018 20:51
-
-
Save meetingcpp/7963d7ad10700f7be8175900235d6fd3 to your computer and use it in GitHub Desktop.
fusion visit & assign
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
| namespace detail{ | |
| template <size_t I> | |
| struct fusion_visit_impl | |
| { | |
| template <typename Seq, typename F> | |
| static void visit(Seq& s, size_t idx, F&& fun) | |
| { | |
| static_assert(boost::fusion::result_of::size<Seq>::value >= I,"fusion index out of bounds"); | |
| if (idx == I - 1) fun(boost::fusion::get<I - 1>(s)); | |
| else fusion_visit_impl<I - 1>::visit(s, idx, std::forward<F>(fun)); | |
| } | |
| }; | |
| template <> | |
| struct fusion_visit_impl<0> | |
| { | |
| template <typename Seq, typename F> | |
| static void visit(Seq& , size_t , F&& ) { } | |
| }; | |
| } | |
| template <typename F, typename Seq> | |
| void visit_fusion_sequence_at(Seq const& s, size_t idx, F&& fun) | |
| { | |
| detail::fusion_visit_impl<boost::fusion::result_of::size<Seq>::value>::visit(s, idx, std::forward<F>(fun)); | |
| } | |
| template <typename F, typename Seq> | |
| void visit_fusion_sequence_at(Seq & s, size_t idx, F&& fun) | |
| { | |
| detail::fusion_visit_impl<boost::fusion::result_of::size<Seq>::value>::visit(s, idx, std::forward<F>(fun)); | |
| } | |
| //The F [&s](auto& x){assign(s,x);} | |
| template<class T, class V> | |
| typename std::enable_if< std::is_convertible< T,V >::value ,void>::type assign(T& to, const V& from) | |
| { | |
| to = from; | |
| } | |
| // linker error when type conversion is not possible | |
| template< class T, class V> | |
| typename std::enable_if< !std::is_convertible< T,V >::value ,void>::type assign(T& , const V& );//{} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment