fork download
  1. #include <tuple>
  2. #include <type_traits>
  3.  
  4. template< class T, class U = typename std::remove_cv< typename std::remove_reference< T >::type >::type >
  5. struct remove_const_and_reference : remove_const_and_reference< U >
  6. {
  7. };
  8.  
  9. template< class T >
  10. struct remove_const_and_reference< T, T >
  11. {
  12. typedef T type;
  13. };
  14.  
  15. class A
  16. {
  17. public:
  18. template< class Ret, class... A >
  19. void Call( Ret f( A... ) ) { CallCore( f ); }
  20.  
  21. template< class Ret, class Obj, class... A >
  22. void Call( Ret( Obj::*f )( A...) ) { CallCore( f ); }
  23.  
  24. private:
  25. template< class Ret, class... A >
  26. void CallCore( Ret f( A... ) )
  27. {
  28. using tuple_t = std::tuple< typename remove_const_and_reference< A >::type... >;
  29. }
  30.  
  31. template< class Ret, class Obj, class... A >
  32. void CallCore( Ret( Obj::*f )( A...) )
  33. {
  34. using tuple_t = std::tuple< typename remove_const_and_reference< A >::type... >;
  35. }
  36. };
  37.  
  38. template< class T, class... A >
  39. T* New( A&&... args )
  40. {
  41. return new T( std::forward< A >( args )... );
  42. }
  43.  
  44. class B
  45. {
  46. public:
  47. template< class T, class... A >
  48. void RegisterConstructor()
  49. {
  50. a.Call< T* >( New< T, A... > );
  51. }
  52.  
  53. template< class Fun >
  54. void Register( Fun f )
  55. {
  56. a.Call( f );
  57. }
  58.  
  59. private:
  60. A a;
  61. };
  62.  
  63. struct Foo
  64. {
  65. Foo(){}
  66. ~Foo(){}
  67. void FunA( int ){}
  68. };
  69.  
  70. void FunB( int ){}
  71.  
  72. int main()
  73. {
  74. B b;
  75. b.Register( FunB );
  76. b.Register( &Foo::FunA );
  77. b.RegisterConstructor< Foo >();
  78. }
Success #stdin #stdout 0s 3340KB
stdin
Standard input is empty
stdout
Standard output is empty