-
Notifications
You must be signed in to change notification settings - Fork 0
/
call_traits.h
68 lines (52 loc) · 1.87 KB
/
call_traits.h
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#pragma once
/*Determine return type, number of arguments and argument types of a callable type (function or function object).
Usage:
int foo() { return 42; }
using FooReturnType = call_traits<decltype(foo)>::return_type;
struct Bar
{
float method(int) { return 3.141592654; }
};
using BarMethodReturnType = call_traits<decltype(&Bar::method)>::return_type;
const auto BarMethodArgCount = call_traits<decltype(&Bar::method)>::arg_count;
using BarMethodArg0Type = call_traits<decltype(&Bar::method)>::arg_type<0>;
struct Baz
{
std::string operator()(int) { return {}; }
};
using BazReturnType = call_traits<Baz>::return_type;
const auto BazArgCount = call_traits<Baz>::arg_count;
using BazArg0Type = call_traits<Baz>::arg_type<0>;
auto lambda = [](int, char){ return 23; };
using LambdaReturnType = call_traits<decltype(lambda)>::return_type;
const auto LambdaArgCount = call_traits<decltype(lambda)>::arg_count;
using LambdaArg0Type = call_traits<decltype(lambda)>::arg_type<0>;
using LambdaArg1Type = call_traits<decltype(lambda)>::arg_type<1>;
*/
#include <tuple>
template<typename F>
struct call_traits;
template<typename R, typename... Args>
struct call_traits<R(Args...)>
{
using return_type = R;
static constexpr size_t arg_count = sizeof...(Args);
template<size_t Index>
using arg_type = std::tuple_element_t<Index, std::tuple<Args...>>;
};
template<typename R, typename... Args>
struct call_traits<R(*)(Args...)> : call_traits<R(Args...)>
{
};
template<typename R, typename F, typename... Args>
struct call_traits<R(F::*)(Args...)> : call_traits<R(Args...)>
{
};
template<typename R, typename F, typename... Args>
struct call_traits<R(F::*)(Args...) const> : call_traits<R(Args...)>
{
};
template<typename F>
struct call_traits : call_traits<decltype(&F::operator())>
{
};