如何正确使用 std::function 作为谓词
How to properly use std::function as a predicate
我想在模板化辅助函数中使用 std::function 作为谓词,但出现错误:
main.cpp(15): error C2672: 'Any': no matching overloaded function found main.cpp(15): error C2784: 'bool Any(const std::vector<T,std::allocator<_Ty>> &,const std::function<bool(const T &)>)': could not deduce template argument for 'const std::function<bool(const T &)>' from 'main::<lambda_1b47ff228a1af9c86629c77c82b319f9>' main.cpp(7): note: see declaration of 'Any'
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
template <typename T>
bool Any(const std::vector<T>& list, const std::function<bool(const T&)> predicate)
{
return std::any_of(list.begin(), list.end(), [&](const T& t) { return predicate(t); });
}
int main()
{
auto myList = std::vector<int>{ 1, 2, 3, 4, 5 };
if (Any(myList, [](int i) { return i % 5 == 0; }))
{
std::cout << "Bingo!" << std::endl;
}
else
{
std::cout << "Not found :(" << std::endl;
}
return 0;
}
我不明白我做错了什么。
我不明白我做错了什么。
您正在将 lambda 传递给Any
,但隐式转换(从 lambda 到std::function
(不会在模板参数推导中考虑,这使得无法在第二个函数参数上推断T
。
推断不考虑隐式转换(上面列出的类型调整除外(:这是重载解决的工作,稍后会发生。
您可以使用std::type_identity
(自 C++20 起(从推导中排除第二个参数;然后模板参数T
将仅在第一个参数上推导,然后代码工作正常。
template <typename T>
bool Any(const std::vector<T>& list, const std::function<bool(const std::type_identity_t<T>&)> predicate)
// ^^^^^^^^^^^^^^^^^^^^^^^
{
return std::any_of(list.begin(), list.end(), [&](const T& t) { return predicate(t); });
}
住
如果你不能申请C++20,你可以参考上面的链接页面,它给出了一个可能的实现,实现你自己的type_identity
并不难。如果你不坚持std::function
,你可以为谓词添加另一个模板参数。
template <typename T, typename P>
bool Any(const std::vector<T>& list, P predicate)
{
return std::any_of(list.begin(), list.end(), predicate);
}
住
或者使其更通用地使用其他容器或原始数组。
template <typename C, typename P>
bool Any(const C& c, P predicate)
{
return std::any_of(std::begin(c), std::end(c), predicate);
}
住
正确的代码:
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
template <typename T>
bool Any(const std::vector<T>& list, const std::function<bool(const T&)> predicate)
{
return std::any_of(list.begin(), list.end(), [&](const T& t) { return predicate(t); });
}
int main()
{
auto myList = std::vector<int>{ 1, 2, 3, 4, 5 };
if (Any<int>(myList, [](int i) -> bool { return i % 5 == 0; }))
{
std::cout << "Bingo!" << std::endl;
}
else
{
std::cout << "Not found :(" << std::endl;
}
return 0;
}
if (Any<int>(myList, [](int i) -> bool { return i % 5 == 0; }))
- 您需要指定模板参数(任何(
- 您忘记添加 lambda 的返回类型
相关文章:
- "error: no matching function for call to"构造函数错误
- std::condition_variable::wait()如何评估给定的谓词
- 调用专用模板时出错"no matching function for call to [...]"
- 有没有办法将谓词中的元素偏移量传递给 std 算法?
- 库函数需要一个 std::function<void(void)>,如何传入类函数?
- Confusion: decltype vs std::function
- 为什么 std::function 可以作为 std::not2 的参数?
- 'max'匹配'std::function<const int &(const int &, const int &)>'无过载
- 传递给std::function template的template参数究竟代表什么
- 将带有unique_ptr的可变 lambda 传递给 const&std::function
- 绑定派生类方法C++从实例范围之外的分隔 std::function 变量调用
- "no matching function for call to 'Vector::Vector'"错误
- 如何在向量中删除 std::function<void()>?
- 不断"Attempting to reference a deleted function"
- 将函数包装器转换为 std::function
- 类型擦除的std::function与虚拟函数调用的开销
- C++ std::function 对于类 exept 的所有实例都是空的(只有 Visual2019 编译器问题)
- 如果模板没有可变参数,则 Lambda 被推导出为 std::function
- 我在 ifstream input_file(文件名)行中收到错误"no matching function to call";
- 如何正确使用 std::function 作为谓词