/
closure_cpp11.hpp
55 lines (43 loc) · 976 Bytes
/
closure_cpp11.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#pragma once
#include <utility>
#include <type_traits>
#include <tuple>
#include "decay_copy.hpp"
#include "apply_from_tuple_cpp11.hpp"
namespace test
{
namespace detail
{
template<typename Function, typename... Args>
struct closure
{
closure(const Function &f, const Args&... args)
: f(f),
args(args...)
{}
closure(Function &&f, Args&&... args)
: f(std::forward<Function>(f)),
args(std::forward<Args>(args)...)
{}
void operator()()
{
apply_from_tuple(f, args);
}
Function f;
std::tuple<Args...> args;
};
template<typename Function, typename... Args>
closure<
typename std::decay<Function>::type,
typename std::decay<Args>::type...
>
make_closure(Function &&f, Args&&... args)
{
typedef closure<
typename std::decay<Function>::type,
typename std::decay<Args>::type...
> result_type;
return result_type(std::forward<Function>(f),std::forward<Args>(args)...);
}
} // end detail
} // end test