Cal*_*ead 3 c++ lambda functional-programming
我正在尝试用 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;
}
};
Run Code Online (Sandbox Code Playgroud)
使用代码:
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;
});
}
Run Code Online (Sandbox Code Playgroud)
错误输出:
.../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.
Run Code Online (Sandbox Code Playgroud)
你的函数应该只接受一个简单的类型:
template <typename F, typename OutType = std::invoke_result_t<F, T const&>>
auto map(F function) -> List<OutType>
{
List<OutType> list;
for (u32 index = 0; index < size; index += 1)
{
list.push(function(((T*)buffer)[index]));
}
return list;
}
Run Code Online (Sandbox Code Playgroud)
这样,F
可以是 lambda、函数指针或任何其他可以接收T
.
如果F
将解析为不能用 a 调用的任何其他类型T
,这将是一个替换错误。
在这种情况下,您可以将 lambda 强制转换为函数指针:
auto list2 = list.map(+[](i32 n) -> i32 {
return n + 3;
});
Run Code Online (Sandbox Code Playgroud)
这只有效,因为 lambda 不捕获任何东西。在一般情况下,您的模板应该区分函数指针和可调用事物(具有operator()
定义的事物)。