fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <tuple>
  4.  
  5. const static auto null = nullptr;
  6.  
  7. template<typename TFuncSignature>
  8. class Callback;
  9.  
  10. template<typename R, typename ...A>
  11. class Callback<R (A...)> {
  12. public:
  13. const static size_t Arity = sizeof...(A);
  14.  
  15. typedef R (*TFunc)(void*, A...);
  16.  
  17. Callback() : obj(0), func(0) {}
  18. Callback(void* o, TFunc f) : obj(o), func(f) {}
  19.  
  20. R operator()(A... a) const {
  21. return (*func)(obj, a);
  22. }
  23.  
  24. typedef void* Callback::*SafeBoolType;
  25. operator SafeBoolType () const {
  26. return func != 0? &Callback::obj : 0;
  27. }
  28.  
  29. bool operator! () const {
  30. return func == 0;
  31. }
  32.  
  33. bool operator== ( const Callback<R (A...)>& right ) const {
  34. return obj == right.obj && func == right.func;
  35. }
  36.  
  37. bool operator!= ( const Callback<R (A...)>& right ) const {
  38. return obj != right.obj || func != right.func;
  39. }
  40.  
  41. private:
  42. void* obj;
  43. TFunc func;
  44. };
  45.  
  46. namespace IntStatic {
  47. void VoidTest ( ) { std::cout << "INTO THE VOID" << std::endl; }
  48. void IntTest ( int num) { std::cout << "Got myself a " << num << " !" << std::endl; }
  49. void IntTest2 ( int num) { std::cout << "Now _I_ Got myself a " << num << " !" << std::endl; }
  50. }
  51. struct Int {
  52. void Test ( int num) { std::cout << num << " on the inside of a class... ?" << std::endl; }
  53. void Test2 ( int num) { std::cout << num << " on the inside of a struct, yo !" << std::endl; }
  54. static void Test3 ( int snum) { std::cout << snum << " on the inside of a STATIC method?!" << std::endl; }
  55. };
  56.  
  57. template<typename R, typename... A>
  58. struct DeduceStaticCallbackTag {
  59.  
  60. /* SO THIS IS WHERE THE PROBLEM IS, FUCK YOU VARIADIC TEMPLATES */
  61. // Not really fuck you, I need you. IT'S JUST A COMMENT YOU CAN'T TAKE ME SERIOUSLY
  62.  
  63. // *Ahem.* In either case, `A ...` is an invalid expansion, I.E. it's not allowed
  64. // Is there anyway we can get a variadic function signature out and expanded?
  65. // If not, then there's no way to do this. D:
  66. template< R (* Func)( A ... ) >
  67. static R Wrapper(void*, A... a) {
  68. return (*Func)( a... );
  69. }
  70.  
  71. /*template<R (*Func)( A... )>
  72. inline static Callback<R (A...)> Bind( ) {
  73. return Callback<R (A...)>( 0, &DeduceStaticCallbackTag::Wrapper<Func> );
  74. }*/
  75. };
  76.  
  77. // Save this for when we figure variadic templates out truly
  78. /*template<typename R, typename... A>
  79. DeduceStaticCallbackTag<R, A...> DeduceStaticCallback(R (*)(A...)) {
  80. return DeduceStaticCallbackTag<R, A...>();
  81. }*/
  82.  
  83. int main(int argc, char* argv[]) {
  84.  
  85. Callback<void(int)> m( 0, &DeduceStaticCallbackTag<void, int>::Wrapper<&IntStatic::IntTest> );
  86.  
  87. //DeduceStaticCallback( &IntStatic::IntTest2 ).Bind<&IntStatic::IntTest2>( );
  88.  
  89. /*Event<int> intev;
  90. Int i;
  91. intev.Add<Int, &Int::Test>(i);
  92. intev.Add<&IntStatic::IntTest>();
  93. intev.Add<&IntStatic::IntTest2>();
  94. //intev.Add( Int::Test3 );
  95. intev(20);
  96. intev.Remove<&IntStatic::IntTest>();
  97. intev.Remove<&IntStatic::IntTest>();
  98. intev.Remove<Int, &Int::Test>(i);
  99. //intev.Remove( Int::Test3 );
  100. //intev.Remove( i, &Int::Test );
  101. intev(20);
  102. */
  103.  
  104. return 0;
  105. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Line 60, yo
compilation info
prog.cpp:5:26: error: 'nullptr' was not declared in this scope
prog.cpp:5:26: error: unable to deduce 'const auto' from '<expression error>'
prog.cpp: In member function 'R Callback<R(A ...)>::operator()(A ...) const':
prog.cpp:21:24: error: parameter packs not expanded with '...':
prog.cpp:21:24: note:         'a'
stdout
Standard output is empty