如何获取 lambda 的返回类型,在 C++11 中归约函数
How to get the return type of a lambda, reduce function in C++11
我正在尝试实现一个reduce函数,但我不知道如何获取lambda的返回类型:
template <typename IT, typename F, typename OT = IT>
auto reducef(const IT& input, F func) -> decltype(func(IT::value_type))
{
decltype(func(typename IT::value_type)) result = {};
return std::accumulate(input.begin(), input.end(), result, func);
}
编译器输出如下:
test.cpp: In function ‘int main(int, char**)’:
test.cpp:37:80: error: no matching function for call to ‘reducef(std::vector<int>&, main(int, char**)::<lambda(const int&, const int&)>)’
test.cpp:37:80: note: candidate is:
test.cpp:22:6: note: template<class IT, class F, class OT> decltype (func(IT:: value_type)) reducef(const IT&, F)
test.cpp:22:6: note: template argument deduction/substitution failed:
test.cpp: In substitution of ‘template<class IT, class F, class OT> decltype (func(IT:: value_type)) reducef(const IT&, F) [with IT = std::vector<int>; F = main(int, char**)::<lambda(const int&, const int&)>; OT = std::vector<int>]’:
test.cpp:37:80: required from here
test.cpp:22:6: error: dependent-name ‘IT:: value_type’ is parsed as a non-type, but instantiation yields a type
test.cpp:22:6: note: say ‘typename IT:: value_type’ if a type is meant
扩展用例:
template <typename IT, typename F, typename OT = IT>
OT mapf(const IT& input, F func)
{
OT output;
output.resize(input.size());
std::transform(input.begin(), input.end(), output.begin(), func);
return output;
}
template <typename IT, typename F, typename OT = IT>
auto reducef(const IT& input, F func) -> decltype(func(IT::value_type))
{
typename IT::value_type result = {};
return std::accumulate(input.begin(), input.end(), result, func);
}
int main(int argc, char *argv[])
{
vector<int> v1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
auto v2 = mapf(v1, [](const int& i) { return i+1;});
for (const auto& i : v2)
{
cout << i << endl;
}
cout << reducef(v1, [](const int& i, const int& j) -> int { return i + j; }) << endl;
}
我想你想声明你的返回类型是这样的
decltype(func(std::declval<typename IT::value_type>(),
std::declval<typename IT::value_type>()))
这是一个完整的测试用例:
#include <algorithm>
#include <utility>
#include <vector>
#include <numeric>
template <typename IT, typename F, typename OT = IT>
auto reducef(const IT& input, F func)
-> decltype(func(std::declval<typename IT::value_type>(),
std::declval<typename IT::value_type>()))
{
decltype(func(std::declval<typename IT::value_type>(),
std::declval<typename IT::value_type>())) result{};
return std::accumulate(input.begin(), input.end(), result, func);
}
int main()
{
std::vector<int> values;
reducef(values, [](int a, int b) { return a + b; });
}
相关文章:
- 继承:构造函数,初始化C++11中基类的类C数组成员
- 为什么 -mmacosx-version-min=10.10 不阻止使用标记为从 10.11 开始的函数?
- C++11:模板方法的模板函数调用无法编译?
- 为什么即使调用了析构函数,C++11 中的分离线程也可以执行
- MacOS 上的 Xcode 11 项目不在一个函数中使用 sin 和 cos:未定义的符号"___sincosf_stret"
- 在 C++11 函数中使用尾随返回类型的优点
- 当加入 C++11 函数的线程仍未终止时,是否可以返回?
- 如何使用 c++11 函数回调声明多个模板参数
- 通用 C++11 函数包装器,用于基于任务的并行性
- 有没有办法<int><double>在使用 C++11 函数调用期间自动将"向量"提升为"向量"?
- 如何使C++11函数生效<>参数自动接受lambdas
- 如何将可变数量的不同类型的参数传递给 C++11 函数映射中的函数
- 看看c++11函数是如何实现的
- Eclipse Luna 无法解析 c++11 函数"stoi"
- 用可变的std::function的c++ 11函数签名
- 使用NSS调用pkcs#11函数
- 从现代C++11函数转换为原始函数指针
- c++ 11函数返回时会发生什么
- 带有auto关键字的c++ 11函数定义
- __func__ C++11 函数的局部预定义变量,无法编译