From 7b9a843e2db83237e56c64ed72a826b1cde67ff9 Mon Sep 17 00:00:00 2001 From: Nicolas Clauvelin Date: Tue, 13 Mar 2018 17:29:15 -0400 Subject: [PATCH] ADD MORE FUNCTIONALITY TO THE TEMPLATE FOR LOOP IMPLEMENTATION This adds a C++14 apply method that can be used with polymorphic lambdas. --- register/RegisterPack.h | 45 ++++++++++++++++++++++++++++++----------- 1 file changed, 33 insertions(+), 12 deletions(-) diff --git a/register/RegisterPack.h b/register/RegisterPack.h index 266f4d8..3b416fc 100644 --- a/register/RegisterPack.h +++ b/register/RegisterPack.h @@ -176,26 +176,47 @@ namespace cppreg { //! Loop method. /** - * @tparam Op Operation to be called at each iteration. + * @tparam Func Function to be called at each iteration. * * This will call Op for the range [start, end). */ - template - inline static void loop( - typename std::enable_if::type* = nullptr - ) { - Op().template operator()(); + template + inline static void loop() { + Func().template operator()(); if (start < end) - for_loop::template loop(); + for_loop::template loop(); }; - //! Loop method closure. - template - inline static void loop( - typename std::enable_if= end, T>::type* = nullptr - ) {}; +#if __cplusplus >= 201402L + //! Apply method. + /** + * @tparam Op Operator type to be called. + * + * This is only available with C++14 and up as this requires polymorphic + * lambdas to be used in a somewhat useful manner. + * + * Typical example: + * use lambda [](auto index) { index.value will be the loop index}; + */ + template + inline static void apply(Op& f) { + if (start < end) { + f(std::integral_constant{}); + for_loop::apply(f); + }; + }; +#endif // __cplusplus 201402L }; + template + struct for_loop { + template + inline static void loop() {}; +#if __cplusplus >= 201402L + template + inline static void apply(Op& f) {}; +#endif // __cplusplus 201402L + }; //! Template range loop implementation.