namespace folly {
+namespace detail {
+namespace partial {
+
+// helper type to make sure that the templated constructor in Partial does
+// not accidentally act as copy or move constructor
+struct PartialConstructFromCallable {};
+
template <typename F, typename Tuple>
class Partial {
private:
public:
template <typename Callable, typename... Args>
- Partial(Callable&& callable, Args&&... args)
+ Partial(PartialConstructFromCallable, Callable&& callable, Args&&... args)
: f_(std::forward<Callable>(callable)),
stored_args_(std::forward<Args>(args)...) {}
}
};
+} // namespace partial
+} // namespace detail
+
/**
* Partially applies arguments to a callable
*
* and passed to the original callable.
*/
template <typename F, typename... Args>
-auto partial(F&& f, Args&&... args) -> Partial<
+auto partial(F&& f, Args&&... args) -> detail::partial::Partial<
typename std::decay<F>::type,
std::tuple<typename std::decay<Args>::type...>> {
- return {std::forward<F>(f), std::forward<Args>(args)...};
+ return {detail::partial::PartialConstructFromCallable{},
+ std::forward<F>(f),
+ std::forward<Args>(args)...};
}
} // namespace folly
#include <memory>
+#include <folly/Function.h>
#include <folly/Partial.h>
#include <folly/portability/GTest.h>
EXPECT_EQ(560, *result);
}
+
+TEST(Partial, WrapInStdFunction) {
+ auto p1 = partial(&add3, 2);
+ std::function<int(int, int)> func = p1;
+ EXPECT_EQ(234, func(3, 4));
+}
+
+TEST(Partial, WrapInFollyFunction) {
+ auto p1 = partial(&add3, 2);
+ folly::Function<int(int, int)> func = p1;
+ EXPECT_EQ(234, func(3, 4));
+}