表示函数参数的元组的类型表达式

Type expression for a tuple representing arguments to a function

本文关键字:类型 表达式 元组 表示 参数 函数      更新时间:2023-10-16

我正在寻找一种方法,为表示函数所需参数的std::元组创建类型表达式。考虑以下内容:

template<typename F, typename ...args>
void myfunction(F&& function, A... args)
{
    std::tuple</*???*/> arguments;
    populate_tuple(arguments,args...);
    return apply(function,arguments);
}

其中,F是一个普通函数类型,apply()是一个将参数应用于函数的函数,populate_tuple()在用函数调用的最终参数填充元组之前,对参数进行一些处理(包括类型转换)。

注意:我不能在元组的声明中使用args...,因为这些是而不是函数所期望的类型-populate_tuple()进行转换。

在我看来,编译器拥有完成这项工作所需的一切,但我不知道语言是否支持它。有什么想法吗?感谢所有的帮助。

也许是这样的东西:

template <typename T> struct TupleOfArguments;
template <typename R, typename ... Args>
struct TupleOfArguments<R(Args...)> {
  typedef std::tuple<Args...> type;
};

演示

这行得通吗?

template<typename F, typename ...Args>
void myfunction(F&& function, Args&&... args) {
     return apply(std::forward<F>(function), std::make_tuple(std::forward<Args>(args)...));
}
相关文章: