fork(2) download
  1. #include <iostream>
  2. #include <functional>
  3. #include <string>
  4.  
  5. namespace fx {
  6. template<int I> struct placeholder{};
  7. }
  8.  
  9. namespace std{
  10. template<int I>
  11. struct is_placeholder< ::fx::placeholder<I>> : std::integral_constant<int, I>{};
  12. }
  13.  
  14. namespace fx {
  15. template <size_t... Is>
  16. struct indices {};
  17.  
  18. template <size_t N, std::size_t... Is>
  19. struct build_indices : build_indices<N-1, N-1, Is...> {};
  20.  
  21. template <size_t... Is>
  22. struct build_indices<0, Is...> : indices<Is...> {};
  23.  
  24. template<std::size_t... Is, class F, class... Args>
  25. auto easy_bind(indices<Is...>, F const& f, Args&&... args)
  26. -> decltype(std::bind(f, std::forward<Args>(args)..., placeholder<Is + 1>{}...))
  27. {
  28. return std::bind(f, std::forward<Args>(args)..., placeholder<Is + 1>{}...);
  29. }
  30.  
  31. template<class R, class... FArgs, class... Args>
  32. auto easy_bind(std::function<R(FArgs...)> f, Args&&... args)
  33. -> decltype(fx::easy_bind(build_indices<sizeof...(FArgs) - sizeof...(Args)>{}, f, std::forward<Args>(args)...)) {
  34. return fx::easy_bind(build_indices<sizeof...(FArgs) - sizeof...(Args)>{}, f, std::forward<Args>(args)...);
  35. }
  36. }
  37.  
  38. //Test Case:
  39. struct SomeStruct {
  40. public:
  41. void function(int x, float y, std::string str) {
  42. std::cout << x << " " << y << " " << str << std::endl;
  43. }
  44. };
  45.  
  46. using Functor = std::function<void(SomeStruct*,int,float,std::string)>;
  47.  
  48. int main() {
  49. Functor functor = &SomeStruct::function;
  50. auto func1 = fx::easy_bind(functor, new SomeStruct);
  51. func1(5, 2.5, "Test1");
  52.  
  53. auto func2 = fx::easy_bind(Functor(&SomeStruct::function), new SomeStruct);
  54. func2(5, 2.5, "Test2");
  55.  
  56. auto func3 = fx::easy_bind(&SomeStruct::function, new SomeStruct);
  57. func3(5, 2.5, "Test3");
  58.  
  59. return 0;
  60. }
Compilation error #stdin compilation error #stdout 0s 3432KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘int main()’:
prog.cpp:56:69: error: no matching function for call to ‘easy_bind(void (SomeStruct::*)(int, float, std::string), SomeStruct*)’
     auto func3 = fx::easy_bind(&SomeStruct::function, new SomeStruct);
                                                                     ^
prog.cpp:56:69: note: candidates are:
prog.cpp:25:10: note: template<unsigned int ...Is, class F, class ... Args> decltype (std::bind(f, (forward<Args>)(fx::easy_bind::args)..., {}...)) fx::easy_bind(fx::indices<Is ...>, const F&, Args&& ...)
     auto easy_bind(indices<Is...>, F const& f, Args&&... args)
          ^
prog.cpp:25:10: note:   template argument deduction/substitution failed:
prog.cpp:56:69: note:   ‘void (SomeStruct::*)(int, float, std::string) {aka void (SomeStruct::*)(int, float, std::basic_string<char>)}’ is not derived from ‘fx::indices<Is ...>’
     auto func3 = fx::easy_bind(&SomeStruct::function, new SomeStruct);
                                                                     ^
prog.cpp:32:10: note: template<class R, class ... FArgs, class ... Args> decltype (fx::easy_bind(fx::build_indices<(sizeof (FArgs ...) - sizeof (Args ...))>{}, f, (forward<Args>)(fx::easy_bind::args)...)) fx::easy_bind(std::function<_Res(_ArgTypes ...)>, Args&& ...)
     auto easy_bind(std::function<R(FArgs...)> f, Args&&... args)
          ^
prog.cpp:32:10: note:   template argument deduction/substitution failed:
prog.cpp:56:69: note:   ‘void (SomeStruct::*)(int, float, std::string) {aka void (SomeStruct::*)(int, float, std::basic_string<char>)}’ is not derived from ‘std::function<_Res(_ArgTypes ...)>’
     auto func3 = fx::easy_bind(&SomeStruct::function, new SomeStruct);
                                                                     ^
stdout
Standard output is empty