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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//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);
}

  

posted on   patton88  阅读(372)  评论(0编辑  收藏  举报

编辑推荐:
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
阅读排行:
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战

导航

< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8
点击右上角即可分享
微信分享提示