【问题标题】:Is it allowed to pass the address of a CPO as an algorithm parameter?是否允许将 CPO 的地址作为算法参数传递?
【发布时间】:2021-01-18 10:38:51
【问题描述】:

我记得在某处读到自 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);
}

【问题讨论】:

    标签: c++ c++20


    【解决方案1】:

    是的,可以将 CPO 作为参数传递。它们是具有静态存储时长的对象,类似于std::cout 等。

    难以将重载函数作为参数传递是 CPO 存在的主要原因之一。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-31
      相关资源
      最近更新 更多