gpt4 book ai didi

c++ - 接收参数的 lambda 函数类型

转载 作者:行者123 更新时间:2023-11-30 03:14:18 24 4
gpt4 key购买 nike

我正在尝试接收 lambda 函数作为参数,但我遇到了它的类型问题。

下面是我调用函数的方式

profile(0, 1000, fps, [](ProfilerVariable<int> &fps){fps.counter++;}, [](ProfilerVariable<int> &fps){fps.counter=0;});

我是这样定义的:

template <typename T>
void profile(int index, int intervalInMilliseconds, ProfilerVariable<T>& profilerVariable,
std::function<void(ProfilerVariable<T>&)> const &increaseFunction,
std::function<void(ProfilerVariable<T>&)> const &resetFunction)
{
increaseFunction(profilerVariable);
//...

我看不出有什么不妥。返回类型为 void,它接受 ProfilerVariable& :

    /home/dev/orwell/cpp/common/ZLRTSPClient.cpp:38:26: error: no matching function for call to 'ZLRTSPClient::profile(int, int, ProfilerVariable<int>&, ZLRTSPClient::init()::<lambda(const toolkit::SockException&)>::<lambda(const Ptr&)>::<lambda(ProfilerVariable<int>&)>, ZLRTSPClient::init()::<lambda(const toolkit::SockException&)>::<lambda(const Ptr&)>::<lambda(ProfilerVariable<int>&)>)'
});
^
In file included from /home/dev/orwell/cpp/common/RTSPClient.h:11:0,
from /home/dev/orwell/cpp/common/ZLRTSPClient.h:3,
from /home/dev/orwell/cpp/common/ZLRTSPClient.cpp:1:
/home/dev/orwell/cpp/common/Profiler.h:41:10: note: candidate: template<class T> void Profiler::profile(int, int, ProfilerVariable<T>&, const std::function<void(ProfilerVariable<T>&)>&, const std::function<void(ProfilerVariable<T>&)>&)
void profile(int index, int intervalInMilliseconds, ProfilerVariable<T>& profilerVariable,
^~~~~~~
/home/dev/orwell/cpp/common/Profiler.h:41:10: note: template argument deduction/substitution failed:
/home/dev/orwell/cpp/common/ZLRTSPClient.cpp:38:26: note: 'ZLRTSPClient::init()::<lambda(const toolkit::SockException&)>::<lambda(const Ptr&)>::<lambda(ProfilerVariable<int>&)>' is not derived from 'const std::function<void(ProfilerVariable<T>&)>'
});
^

PS:const 是什么意思?在std::function<void(ProfilerVariable<T>&)> const &increaseFunction代表什么?

最佳答案

lambda 不是 std::function,因此无法推导 T

你可以摆脱 std::function 并采用仿函数:

template <typename T, typename F1, typename >
void profile(int index,
int intervalInMilliseconds,
ProfilerVariable<T>& profilerVariable,
F1 const &increaseFunction,
F2 const &resetFunction) {/*..*/}

或者您可以使这些参数不可推导:

template <typename T>
void profile(int index,
int intervalInMilliseconds,
ProfilerVariable<T>& profilerVariable, // Deduce T only here
std::type_identity_t<std::function<void(ProfilerVariable<T>&)>> const &increaseFunction,
std::type_identity_t<std::function<void(ProfilerVariable<T>&)>> const &resetFunction)

std::type_identity_t是 C++20,但可以为以前的版本实现。

否则你必须在调用站点中更改

  • 明确:

    profile<int>(0, 1000, fps, [](ProfilerVariable<int> &fps){fps.counter++;}, [](ProfilerVariable<int> &fps){fps.counter=0;});
  • 或使用“正确”的参数

    profile(0,
    1000,
    fps,
    std::function{[](ProfilerVariable<int> &fps){fps.counter++;}},
    std::function{[](ProfilerVariable<int> &fps){fps.counter=0;}});

关于c++ - 接收参数的 lambda 函数类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57917993/

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