From 0d7a23d34e9e898f538220816704e5add4b03f17 Mon Sep 17 00:00:00 2001 From: Benoit Steiner Date: Thu, 29 Oct 2015 17:29:49 -0700 Subject: [PATCH] Extended the reduction code so that reducing an empty set returns the neural element for the operation --- .../Eigen/CXX11/src/Core/util/CXX11Meta.h | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/unsupported/Eigen/CXX11/src/Core/util/CXX11Meta.h b/unsupported/Eigen/CXX11/src/Core/util/CXX11Meta.h index 7cd04a99e..7e4929ff8 100644 --- a/unsupported/Eigen/CXX11/src/Core/util/CXX11Meta.h +++ b/unsupported/Eigen/CXX11/src/Core/util/CXX11Meta.h @@ -252,6 +252,13 @@ template< typename... Ts > struct reduce; +template< + typename Reducer +> struct reduce +{ + constexpr static inline int run() { return Reducer::Identity; } +}; + template< typename Reducer, typename A, @@ -275,8 +282,14 @@ template< /* generic binary operations */ -struct sum_op { template constexpr static inline auto run(A a, B b) -> decltype(a + b) { return a + b; } }; -struct product_op { template constexpr static inline auto run(A a, B b) -> decltype(a * b) { return a * b; } }; +struct sum_op { + template constexpr static inline auto run(A a, B b) -> decltype(a + b) { return a + b; } + static constexpr int Identity = 0; +}; +struct product_op { + template constexpr static inline auto run(A a, B b) -> decltype(a * b) { return a * b; } + static constexpr int Identity = 1; +}; struct logical_and_op { template constexpr static inline auto run(A a, B b) -> decltype(a && b) { return a && b; } }; struct logical_or_op { template constexpr static inline auto run(A a, B b) -> decltype(a || b) { return a || b; } };