Commit f45b792f authored by Aaryaman Sagar's avatar Aaryaman Sagar Committed by Facebook Github Bot

Conditionally compiled for_each with constexpr

Summary:
Code was breaking in versions of C++ where constexpr functions are
required to have one line bodies

Reviewed By: yfeldblum, mzlee

Differential Revision: D5739062

fbshipit-source-id: 6c509f1daf77751d33ce9c173a0d7f1d3bd2a006
parent 9ce176b1
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
#include <type_traits> #include <type_traits>
#include <utility> #include <utility>
#include <folly/Portability.h>
#include <folly/Traits.h> #include <folly/Traits.h>
namespace folly { namespace folly {
...@@ -378,14 +379,14 @@ struct FetchImpl<S, EnableIfRange<S>> { ...@@ -378,14 +379,14 @@ struct FetchImpl<S, EnableIfRange<S>> {
} // namespace for_each_detail } // namespace for_each_detail
template <typename Sequence, typename Func> template <typename Sequence, typename Func>
constexpr Func for_each(Sequence&& range, Func func) { FOLLY_CPP14_CONSTEXPR Func for_each(Sequence&& range, Func func) {
for_each_detail::ForEachImpl<typename std::decay<Sequence>::type>::impl( for_each_detail::ForEachImpl<typename std::decay<Sequence>::type>::impl(
std::forward<Sequence>(range), func); std::forward<Sequence>(range), func);
return func; return func;
} }
template <typename Sequence, typename Index> template <typename Sequence, typename Index>
constexpr decltype(auto) fetch(Sequence&& sequence, Index&& index) { FOLLY_CPP14_CONSTEXPR decltype(auto) fetch(Sequence&& sequence, Index&& index) {
return for_each_detail::FetchImpl<Sequence>::impl( return for_each_detail::FetchImpl<Sequence>::impl(
std::forward<Sequence>(sequence), std::forward<Index>(index)); std::forward<Sequence>(sequence), std::forward<Index>(index));
} }
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
#pragma once #pragma once
#include <folly/Portability.h>
#include <folly/Preprocessor.h> #include <folly/Preprocessor.h>
#include <type_traits> #include <type_traits>
...@@ -79,7 +80,7 @@ namespace folly { ...@@ -79,7 +80,7 @@ namespace folly {
* }); * });
*/ */
template <typename Range, typename Func> template <typename Range, typename Func>
constexpr Func for_each(Range&& range, Func func); FOLLY_CPP14_CONSTEXPR Func for_each(Range&& range, Func func);
/** /**
* The user should return loop_break and loop_continue if they want to iterate * The user should return loop_break and loop_continue if they want to iterate
...@@ -115,7 +116,7 @@ constexpr auto loop_continue = for_each_detail::LoopControl::CONTINUE; ...@@ -115,7 +116,7 @@ constexpr auto loop_continue = for_each_detail::LoopControl::CONTINUE;
* (operator[]) to fetch the required element * (operator[]) to fetch the required element
*/ */
template <typename Sequence, typename Index> template <typename Sequence, typename Index>
constexpr decltype(auto) fetch(Sequence&& sequence, Index&& index); FOLLY_CPP14_CONSTEXPR decltype(auto) fetch(Sequence&& sequence, Index&& index);
} // namespace folly } // namespace folly
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment