我记得在某处读到过,由于C++20不再允许在名称空间std中使用函数的地址,因此可以在不破坏用户代码的情况下添加此类函数的新重载。这也适用于函数对象吗?
特别是,定制点对象可以作为算法的参数传递吗?例如:
#include <vector>
#include <map>
#include <ranges>
#include <range/v3/numeric/accumulate.hpp>
int main()
{
std::map<int, std::vector<int>> m;
return ranges::accumulate(m | std::views::values, 0u, std::plus{}, std::ranges::size);
}发布于 2021-01-18 18:57:50
是的,将CPOs作为参数传递是很好的。它们是具有静态存储持续时间的对象,类似于std::cout等。
难以将重载函数作为参数传递是CPO存在的主要原因之一。
https://stackoverflow.com/questions/65772907
复制相似问题