Advertisement
Guest User

Code 2

a guest
Aug 15th, 2014
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.78 KB | None | 0 0
  1. #include <utility>
  2. #include <locale>
  3.  
  4. namespace FunctionTraits
  5. {
  6.     template<typename T>
  7.     struct Type{
  8.         using type = T;
  9.     };
  10.    
  11.     template<typename T>
  12.     struct FunctionType;
  13.    
  14.     template<typename T>
  15.     struct FunctionType<T(*)()>
  16.     {
  17.         using ReturnType = typename Type<T>::type;
  18.         using ParameterType = void;
  19.         using Signature = ReturnType();
  20.     };
  21.     template<typename T, typename P>
  22.     struct FunctionType<T (*)( P )>
  23.     {
  24.         using ReturnType = typename Type<T>::type;
  25.         using ParameterType = typename Type<P>::type;
  26.         using Signature = ReturnType(*)( ParameterType );
  27.     };
  28.     template<typename T, typename ...P>
  29.     struct FunctionType<T(*)( P... )>
  30.     {
  31.         using ReturnType = T;
  32.         //~ using ParameterType = typename Type<P...>::type; ???
  33.         using Signature = ReturnType(*)( P... );
  34.     };
  35.    
  36.     template<class T, class ...U>
  37.     using Signature = typename FunctionType<T(*)(U...)>::Signature;
  38.    
  39.     template<typename T, typename ...U>
  40.     auto apply_impl( Signature<T, U...> && function, U && ...args ) ->
  41.                                     decltype( function ( std::forward<U...>( args...)) ){
  42.         return function( std::forward<U...>( args... ));
  43.     }
  44.     template<typename T, typename ...U>
  45.     auto apply( T && a, U &&...args ) -> decltype ( apply_impl( std::forward<T>( a ), std::forward<U...>( args... ) ) ){
  46.         using tag = typename FunctionType<T>::Signature;
  47.         return apply_impl( std::forward<T>( a ), std::forward<U...>( args... ) );
  48.     }
  49. }
  50.  
  51. int foo ( int a, int ){
  52.     return a;
  53. }
  54.  
  55. int main()
  56. {
  57.     auto ch = FunctionTraits::apply( ::tolower, 'A' ); //expects 'a'
  58.     auto ch_2 = FunctionTraits::apply( foo, 1, 3 ); //expects 1
  59.     return 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement