Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/141.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.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++_Templates_Template Meta Programming - Fatal编程技术网

C++ 如何定义依赖于参数包转换的函数的返回类型?

C++ 如何定义依赖于参数包转换的函数的返回类型?,c++,templates,template-meta-programming,C++,Templates,Template Meta Programming,我正在尝试编写一个模板函数,它接受可变数量的对作为输入,将一些函数应用于每个“第一”成员和每个“第二”成员,并返回结果对。我成功地编写了函数本身,但无法自动推断返回类型。如何使用的std::result_获得所需的结果 我的尝试如下: template<typename Output, typename Func, typename... Inputs> std::pair<Output, Output> fmap(Func&& f, Inputs&

我正在尝试编写一个模板函数,它接受可变数量的对作为输入,将一些函数应用于每个“第一”成员和每个“第二”成员,并返回结果对。我成功地编写了函数本身,但无法自动推断返回类型。如何使用的
std::result_获得所需的结果

我的尝试如下:

template<typename Output, typename Func, typename... Inputs>
std::pair<Output, Output> fmap(Func&& f, Inputs&&... inputs)
{
  using Out = typename std::result_of<Func(decltype(inputs.first)...)>::type;
  return std::pair<Out, Out>(f((inputs.first)...),
                             f((inputs.second)...));
  // Here I would like Out to be the same type as Output
}

int add(int i, int j)
{
  return i + j;
}

int main()
{
  std::pair<int, int> pair{1, 2};
  std::pair<int, int> pair2{4, 5};
  auto res = fmap(add, pair, pair2);
  // Crashes with template argument deduction failed, couldn't deduce Output
  std::cout << res2.first << " " << res2.second << std::endl;
  return 0;
}
模板
标准::成对fmap(函数和函数、输入和…输入)
{
使用Out=typename std::result\u of::type;
返回std::pair(f((inputs.first)…),
f((第二次输入)…);
//在这里,我希望输出的类型与输出的类型相同
}
整数相加(整数i,整数j)
{
返回i+j;
}
int main()
{
std::对{1,2};
std::对pair2{4,5};
自动恢复=fmap(添加、配对、配对2);
//模板参数推断失败导致崩溃,无法推断输出

我想这就是你想要的

template<typename Func, typename... Inputs>
auto fmap(Func&& f, Inputs&&... inputs) 
-> std::pair<typename std::result_of<Func(decltype(inputs.first)...)>::type, typename std::result_of<Func(decltype(inputs.first)...)>::type>
{
  using Out = typename std::result_of<Func(decltype(inputs.first)...)>::type;
  return std::pair<Out, Out>(f((inputs.first)...),
                             f((inputs.second)...));
}
模板
自动fmap(功能和功能、输入和…输入)
->std::pair
{
使用Out=typename std::result\u of::type;
返回std::pair(f((inputs.first)…),
f((第二次输入)…);
}

IMO使用
make\u-pair
使其更清洁:

template<typename Func, typename... Inputs>
auto fmap(Func&& f, Inputs&&... inputs) 
    -> std::pair<typename std::result_of<Func(decltype(inputs.first)...)>::type, 
                 typename std::result_of<Func(decltype(inputs.first)...)>::type> // not needed in C++17
{
    return std::make_pair(f((inputs.first)...), f((inputs.second)...));
}
模板
自动fmap(功能和功能、输入和…输入)
->std::pair//在C++17中不需要
{
return std::make_pair(f((inputs.first))、f((inputs.second));
}


如果C++我记得正确,C++可以使用<代码>自动<代码>,你不必定义返回类型,它将自动推断。并且使用(或类似的)来提供一个例子,这将是有益的。谢谢这正是我需要的。完全忘记了“->”语法。