template - function parameter - type

发布时间 2023-06-19 10:01:32作者: ploolq
#include <iostream>
#include <thread>
#include <array>
#include <functional>

template <auto func, std::size_t I>
struct param_type;

template <typename Ret, typename... Args, Ret (*func)(Args...), std::size_t I>
struct param_type<func, I>
{
    using type = std::tuple_element_t<I, std::tuple<Args...>>;
};

template <typename Ret, typename... Args, Ret (*func)(Args..., ...), std::size_t I>
struct param_type<func, I>
{
    using type = std::tuple_element_t<I, std::tuple<Args...>>;
};


void foo(std::string const&, int, char&) {
    //...
}

static_assert(std::is_same_v<const std::string&, param_type<&foo, 0>::type>);
static_assert(std::is_same_v<int, param_type<&foo, 1>::type>);
static_assert(std::is_same_v<char&, param_type<&foo, 2>::type>);

https://stackoverflow.com/questions/70612294/getting-parameter-type-of-function-with-templates