From 96ef7423441d32d8919142abff806fbdf7ed2d90 Mon Sep 17 00:00:00 2001 From: Olivier Parcollet Date: Tue, 18 Feb 2014 15:30:03 +0100 Subject: [PATCH] c14, tuples : various fixes. - Various details (for_each_enumerate impl) - also put all tuple stuff in tuple_tools.hpp - add std14 namespace for usage of c++14 helper types --- test/triqs/utility/tuple_tools.cpp | 22 +++++++++++++++ triqs/utility/c14.hpp | 21 +++----------- triqs/utility/tuple_tools.hpp | 44 ++++++++++++++++++++++-------- 3 files changed, 58 insertions(+), 29 deletions(-) diff --git a/test/triqs/utility/tuple_tools.cpp b/test/triqs/utility/tuple_tools.cpp index 661f84e7..70b9360f 100644 --- a/test/triqs/utility/tuple_tools.cpp +++ b/test/triqs/utility/tuple_tools.cpp @@ -23,6 +23,7 @@ #include #include #include +#include #include struct fun { @@ -107,6 +108,24 @@ int main(int argc, char **argv) { if ( std::abs((res - 35.6)) > 1.e-13) throw std::runtime_error(" "); } + { + // ex of real code + auto fl = [](int i, std::string s) { + auto r = std::to_string(i); + return s.empty() ? r : r + "_" + s; + }; +#ifdef __cpp_generic_lambdas + auto _name = [fl](auto... is) { + auto t = std::make_tuple(is...); + return triqs::tuple::fold(fl, t, std::string{}); + }; + auto r= _name(1,2,3); +#else + auto r = triqs::tuple::fold(fl, reverse(std::make_tuple(1,2,3)), std::string{}); +#endif + std::cerr << r << std::endl; + } + std::cerr << " ----- apply ----"<< std::endl ; { auto res = triqs::tuple::apply(my_print_str, std::make_tuple(1,2)); @@ -182,5 +201,8 @@ int main(int argc, char **argv) { std::cout << "replace 1,3,5"<< t << triqs::tuple::replace<1,3,5>(t,s)<< std::endl; } + + + } diff --git a/triqs/utility/c14.hpp b/triqs/utility/c14.hpp index ff7f7e91..9a6a319c 100644 --- a/triqs/utility/c14.hpp +++ b/triqs/utility/c14.hpp @@ -21,9 +21,7 @@ #pragma once #include #include -#include #include "./macros.hpp" -#include "./tuple_tools.hpp" // a few that will be C++14, use in advance.... @@ -36,6 +34,7 @@ namespace std { template using remove_reference_t = typename remove_reference::type; template using add_const_t = typename add_const::type; template using remove_const_t = typename remove_const::type; + template using decay_t = typename decay::type; template using enable_if_t = typename enable_if::type; // use simply std::c14::plus<>() ... @@ -49,21 +48,9 @@ namespace std { template std::unique_ptr make_unique(Args&&... args) { return std::unique_ptr(new T(std::forward(args)...)); } - // a little helper class to wait for the correction that tuple construct is NOT explicit - template - class tuple : public std::tuple { - public : - template - tuple(Args2 && ... args2) : std::tuple (std::forward(args2)...){} - }; - } - - // minimal hack to get the metaprogramming work with this tuple too.... - template - auto get(c14::tuple const & t) DECL_AND_RETURN( std::get(static_cast>(t))); - - template class tuple_size>: public tuple_size>{}; - } +namespace std14 { + using namespace std::c14; +} diff --git a/triqs/utility/tuple_tools.hpp b/triqs/utility/tuple_tools.hpp index 2fcd748b..d9deebe7 100644 --- a/triqs/utility/tuple_tools.hpp +++ b/triqs/utility/tuple_tools.hpp @@ -22,6 +22,7 @@ #define TRIQS_UTILITY_TUPLE_TOOLS_H #include #include +#include "./c14.hpp" #include // adding to the std lib the reversed lazy tuple... @@ -45,6 +46,7 @@ namespace std { template class tuple_size<_triqs_reversed_tuple> : public tuple_size::type>::type>{}; } + namespace triqs { namespace tuple { /** @@ -140,21 +142,21 @@ namespace triqs { namespace tuple { */ template struct for_each_enumerate_impl { template - void operator()(T const & t, F && f) { - f(std::get::value-1-pos>(t),std::tuple_size::value-1-pos); - for_each_impl()(t, f); + void operator()(T & t, F && f) { + f(std::get>::value-1-pos>(t),std::tuple_size::value-1-pos); + for_each_enumerate_impl()(t, f); } }; template<> struct for_each_enumerate_impl<0> { template - void operator() (T const & t, F && f) { f(std::get::value-1>(t), std::tuple_size::value-1); } + void operator() (T & t, F && f) { f(std::get>::value-1>(t), std::tuple_size>::value-1); } }; template - void for_each_enumerate(T const & t, F && f) { - for_each_enumerate_impl::value-1>()(t, f); + void for_each_enumerate(T & t, F && f) { + for_each_enumerate_impl>::value-1>()(t, f); } /** @@ -250,18 +252,18 @@ namespace triqs { namespace tuple { */ template struct fold_impl { template - auto operator()(F && f, T & t, R && r ) - DECL_AND_RETURN( fold_impl()(std::forward(f),t, f(std::get(t), std::forward(r)))); + auto operator()(F && f, T && t, R && r ) + DECL_AND_RETURN( fold_impl()(std::forward(f),std::forward(t), f(std::get(t), std::forward(r)))); }; template struct fold_impl { - template R operator()(F && f, T & t, R && r) {return std::forward(r);} + template R operator()(F && f, T && t, R && r) {return std::forward(r);} }; template - auto fold (F && f,T & t, R && r) DECL_AND_RETURN( fold_impl::value,std::tuple_size::value-1,T>()(std::forward(f),t,std::forward(r))); - template - auto fold (F && f,T const & t, R && r) DECL_AND_RETURN( fold_impl::value,std::tuple_size::value-1,T const>()(std::forward(f),t,std::forward(r))); + auto fold (F && f,T && t, R && r) DECL_AND_RETURN( fold_impl>::value,std::tuple_size>::value-1,T>()(std::forward(f),std::forward(t),std::forward(r))); + //template + // auto fold (F && f,T const & t, R && r) DECL_AND_RETURN( fold_impl::value,std::tuple_size::value-1,T const>()(std::forward(f),t,std::forward(r))); /** * fold_on_zip(f, t1, t2, init) @@ -451,5 +453,23 @@ namespace std { } } +namespace std { +namespace c14 { + + // a little helper class to wait for the correction that tuple construct is NOT explicit + template class tuple : public std::tuple { + public: + template tuple(Args2 &&... args2) : std::tuple(std::forward(args2)...) {} + }; + } + + // minimal hack to get the metaprogramming work with this tuple too.... + template + auto get(c14::tuple const &t) DECL_AND_RETURN(std::get(static_cast>(t))); + + template class tuple_size> : public tuple_size> {}; + +} + #endif