gpt4 book ai didi

c++ - 函数不会接受 Lambda 但会接受函数指针

转载 作者:行者123 更新时间:2023-12-03 20:42:00 26 4
gpt4 key购买 nike

我正在尝试用 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.

最佳答案

你的函数应该只接受一个简单的类型:

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;
}

这样, F可以是 lambda、函数指针或任何其他可以接收的可调用类型 T .

F将解析为不能用 T 调用的任何其他类型,这将是一个替换错误。

Live example

关于c++ - 函数不会接受 Lambda 但会接受函数指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59671577/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com