Advertisement
Guest User

First code

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