【发布时间】:2020-04-27 12:10:51
【问题描述】:
我正在尝试在 C++ 中实现 JavaScript 映射函数,但无法让它接受 lambda。当我使用函数指针但不使用 lambda 时,它可以工作。我知道 lambda 和函数指针是不同的;我只是不明白为什么 foreach 函数很好但 map 函数不行。
非常感谢您的任何帮助。
template<typename T>
struct List {
void* buffer;
...
void each(void(func)(T))
{
for (u32 index = 0; index < size; index += 1)
{
func(((T*)buffer)[index]);
}
}
template <typename OutType>
List<OutType> map(OutType(func)(T))
{
List<OutType> list;
for (u32 index = 0; index < size; index += 1)
{
list.push(func(((T*)buffer)[index]));
}
return list;
}
};
使用代码:
i64 addTwo(i32 n)
{
return (i64)(n + 2);
}
int main()
{
List<i32> list;
list.push(4);
list.push(2);
// works
list.each([](i32 num) {
std::cout << num << std::endl;
});
// works
auto list1 = list.map(addTwo);
// does not work
auto list2 = list.map([](i32 n) -> i32 {
return n + 3;
});
}
错误输出:
.../main.cpp:53:23: error: no matching member function for call to 'map'
auto list2 = list.map([](i32 n) -> i32 {
~~~~~^~~
.../list.hpp:86:19: note: candidate template ignored: could not match 'OutType (*)(int)' against
'(lambda at /home/caleb/opengl-starter/source/main.cpp:53:27)'
List<OutType> map(OutType(func)(T))
^
1 error generated.
【问题讨论】:
-
lambda 是可调用类型,是的,但它是可调用的,因为它定义了函数调用运算符。
-
除了这里列出的原因之外,其他许多原因无法按原样编译代码。请创建一个实际的minimal reproducible example
-
但它们并不相同。第一个返回 int64。第二个 int32。
-
@AndyG 谢谢,我可能会更新它以更容易重现。我只是在想,只要知道我的错误消息,对 lambda 有更多了解的人就可以轻松回答。
-
@MichaelChourdakis 我也尝试过使用相同的类型。这不应该是错误的根源。
标签: c++ lambda functional-programming