Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/164.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;元组类型到函数参数类型_C++_Tuples_Variadic Templates_Sfinae - Fatal编程技术网

C++ C++;元组类型到函数参数类型

C++ C++;元组类型到函数参数类型,c++,tuples,variadic-templates,sfinae,C++,Tuples,Variadic Templates,Sfinae,假设我有一个函数,它的参数应该与元组的类型相匹配,我可以这样写: using ArgsTuple = std::tuple<int, float>; template<typename... Args, class = typename std::enable_if<std::is_convertible<std::tuple<Args...>, ArgsTuple>::value>::type> void function

假设我有一个函数,它的参数应该与元组的类型相匹配,我可以这样写:

using ArgsTuple = std::tuple<int, float>;

template<typename... Args, 
    class = typename std::enable_if<std::is_convertible<std::tuple<Args...>, ArgsTuple>::value>::type>
void function(Args... args)
{
}
使用ArgsTuple=std::tuple;
模板
无效函数(Args…Args)
{
}

但这实际上不同于将
int
float
作为参数,它接受所有类型的参数,然后限制它们只允许匹配类型。我如何能够以这样的方式解包元组,使函数实际将
ArgsTuple
中的类型作为参数,以便visual studio能够自动完成调用函数所需的类型。结果函数必须是自由函数。

不确定您想要什么,也不完全是自由函数(而是模板类中的静态方法),但我想您要求的是类似于以下内容的内容

template <typename>
struct proFunc;

template <template <typename ...> class C, typename ... Ts>
struct proFunc<C<Ts...>>
 {
   static void func (Ts ...)
    { }
 };
模板
结构形式;
模板
结构形式
{
静态无效函数(Ts…)
{ }
};
你可以用这种方式

using ArgsTuple = std::tuple<int, float>;

proFunc<ArgsTuple>::func(1, 2.f);
使用ArgsTuple=std::tuple;
proFunc::func(1,2.f);
你也可以验证一下

static_assert( std::is_same<decltype(&proFunc<ArgsTuple>::func),
                            void(*)(int, float)>::value, "!" );
static_断言(std::is_same::value,“!”;
要获得更类似于自由函数的内容,可以使用函数指针

auto  funcPnt = &proFunc<ArgsTuple>::func;

funcPnt(3, 4.f);
auto funcPnt=&proFunc::func;
函数(3,4.f);