Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <utility>
- #include <locale>
- namespace FunctionTraits
- {
- template<typename T>
- struct Type{
- using type = T;
- };
- template<typename T>
- struct FunctionType;
- template<typename T>
- struct FunctionType<T(*)()>
- {
- using ReturnType = typename Type<T>::type;
- using ParameterType = void;
- using Signature = ReturnType();
- };
- template<typename T, typename P>
- struct FunctionType<T (*)( P )>
- {
- using ReturnType = typename Type<T>::type;
- using ParameterType = typename Type<P>::type;
- using Signature = ReturnType(*)( ParameterType );
- };
- template<typename T, typename ...P>
- struct FunctionType<T(*)( P... )>
- {
- using ReturnType = T;
- //~ using ParameterType = typename Type<P...>::type; ???
- using Signature = ReturnType(*)( P... );
- };
- template<class T, class ...U>
- using Signature = typename FunctionType<T(*)(U...)>::Signature;
- template<typename T, typename ...U>
- auto apply_impl( Signature<T, U...> && function, U && ...args ) ->
- decltype( function ( std::forward<U...>( args...)) ){
- return function( std::forward<U...>( args... ));
- }
- template<typename T, typename ...U>
- auto apply( T && a, U &&...args ) -> decltype ( apply_impl( std::forward<T>( a ), std::forward<U...>( args... ) ) ){
- using tag = typename FunctionType<T>::Signature;
- return apply_impl( std::forward<T>( a ), std::forward<U...>( args... ) );
- }
- }
- int foo ( int a, int ){
- return a;
- }
- int main()
- {
- auto ch = FunctionTraits::apply( ::tolower, 'A' ); //expects 'a'
- auto ch_2 = FunctionTraits::apply( foo, 1, 3 ); //expects 1
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement