Cppgres
Build Postgres extensions in C++
Loading...
Searching...
No Matches
function_traits.hpp
Go to the documentation of this file.
1
4#pragma once
5
6namespace cppgres::utils::function_traits {
7// Primary template (will be specialized below)
8template <typename T> struct function_traits;
9
10// Specialization for function pointers.
11template <typename R, typename... Args> struct function_traits<R (*)(Args...)> {
12 using argument_types = std::tuple<Args...>;
13 static constexpr std::size_t arity = sizeof...(Args);
14};
15
16// Specialization for function references.
17template <typename R, typename... Args> struct function_traits<R (&)(Args...)> {
18 using argument_types = std::tuple<Args...>;
19 static constexpr std::size_t arity = sizeof...(Args);
20};
21
22// Specialization for function types themselves.
23template <typename R, typename... Args> struct function_traits<R(Args...)> {
24 using argument_types = std::tuple<Args...>;
25 static constexpr std::size_t arity = sizeof...(Args);
26};
27
28// Specialization for member function pointers (e.g. for lambdas' operator())
29template <typename C, typename R, typename... Args>
30struct function_traits<R (C:: *)(Args...) const> {
31 using argument_types = std::tuple<Args...>;
32 static constexpr std::size_t arity = sizeof...(Args);
33};
34
35// Fallback for functors/lambdas that are not plain function pointers.
36// This will delegate to the member function pointer version.
37template <typename T> struct function_traits : function_traits<decltype(&T::operator())> {};
38
39// Primary template (left undefined)
40template <typename Func, typename Tuple> struct invoke_result_from_tuple;
41
42// Partial specialization for when Tuple is a std::tuple<Args...>
43template <typename Func, typename... Args>
44struct invoke_result_from_tuple<Func, std::tuple<Args...>> {
45 using type = std::invoke_result_t<Func, Args...>;
46};
47
48// Convenience alias template.
49template <typename Func, typename Tuple>
50using invoke_result_from_tuple_t = typename invoke_result_from_tuple<Func, Tuple>::type;
51
52} // namespace cppgres::utils::function_traits
Definition: function_traits.hpp:37