• 获得函数返回值类型、参数tuple、成员函数指针中的对象类型


    //function_traits.h,获得函数返回值类型、参数tuple、成员函数指针中的对象类型
    //参考https://github.com/qicosmos/cosmos/blob/master/function_traits.hpp,进行了精简和补充
    #pragma once
    #include <functional>
    #include <tuple>
    
    //类模板原型
    template<typename T>
    struct function_traits;
    
    //普通函数
    template<typename Ret, typename... Args>
    struct function_traits<Ret(Args...)> {
    	typedef Ret return_type;
    	typedef std::tuple<std::remove_const_t<std::remove_reference_t<Args>>...> bare_tuple_type;
    };
    
    //函数指针
    template<typename Ret, typename... Args>
    struct function_traits<Ret(*)(Args...)> : function_traits<Ret(Args...)>{};
    
    //std::function.
    template <typename Ret, typename... Args>
    struct function_traits<std::function<Ret(Args...)>> : function_traits<Ret(Args...)>{};
    
    //成员函数,以及成员函数指针中的对象类型
    #define FUNCTION_TRAITS(...)
    template <typename Ret, typename Obj, typename... Args>
    struct function_traits<Ret(Obj::*)(Args...) __VA_ARGS__> : function_traits<Ret(Args...)>
    { typedef Obj object_type; };
    
    FUNCTION_TRAITS()
    FUNCTION_TRAITS(const)
    FUNCTION_TRAITS(volatile)
    FUNCTION_TRAITS(const volatile)
    
    //函数对象
    template<typename Callable>
    struct function_traits : function_traits<decltype(&Callable::operator())>{};
    
    template <typename Fun>
    typename function_traits<Fun>::stl_function_type to_function(const Fun& lambda) {
    	return static_cast<typename function_traits<Fun>::stl_function_type>(lambda);
    }
    
    template <typename Fun>
    typename function_traits<Fun>::stl_function_type to_function(Fun&& lambda) {
    	return static_cast<typename function_traits<Fun>::stl_function_type>(std::forward<Fun>(lambda));
    }
    
    template <typename Fun>
    typename function_traits<Fun>::pointer to_function_pointer(const Fun& lambda) {
    	return static_cast<typename function_traits<Fun>::pointer>(lambda);
    }
    

      

  • 相关阅读:
    VS2013 自动添加头部注释 -C#开发
    在调用Response.End()时,会执行Thread.CurrentThread.Abort()操作
    React
    WebApi基础
    wcf
    memcached系列
    Ioc容器Autofac系列
    使用TortoiseSVN创建版本库
    使用libcurl 发送post请求
    值得推荐的C/C++框架和库
  • 原文地址:https://www.cnblogs.com/ybmj/p/9651227.html
Copyright © 2020-2023  润新知