language: C++11 (gcc-4.7.2)
date: 185 days 19 hours ago
link:
visibility: public
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
#include <iostream>
#include <tuple>
 
//function traits
template <typename T>
struct function_traits : public function_traits<decltype(&T::operator())>
{};
 
template <typename C, typename R, typename... A>
struct function_traits<R(C::*)(A...) const>
{
   template <size_t i>
   struct arg
   {
      typedef typename std::tuple_element<i, std::tuple<A...>>::type type;
   };
};
 
template<typename InArg, typename Function>
class selfCompose {
  Function f;
 public:
  selfCompose(Function f): f(f) {}
  auto operator() (InArg x) -> decltype(f(f(x))) 
  {
    return f(f(x));                              
  }
};
 
template<typename Fun>
selfCompose<typename function_traits<Fun>::template arg<0>::type, Fun>
make_selfCompose(Fun f)
{
  typedef typename function_traits<Fun>::template arg<0>::type InArg;
  return selfCompose<InArg, decltype(f)>(f);
}
 
int main() {
  auto f = [](int x){return x*x;};
  std::cout << make_selfCompose(f)(4)
            << std::endl;
  return 0;
}