笑看天下无敌手 发表于 2023-2-26 11:01:32

Binary &Op是什么

前言

在并行开发时我们经常会用到Pstream::gather()函数或是全局函数reduce()或者其他,需要输入参数Binary &Op,本篇主要讨论Binary &Op是什么
template<class T, class BinaryOp>
void reduce
(
    T& Value,
    const BinaryOp& bop, // 这里要输入什么参数
    const int tag,
    const label comm,
    label& request
)
{
    NotImplemented;
}Binary &Op

单从名字上看,猜是一个二进制的操作,类似一组操作返回一个二进制的标记
然后去openfoam官网去找,找不到Binary &Op的任何释义
去网上找,发现了一点端倪
c++标准库中有应用Binary &Op这个输入参数,使用的函数也叫reduce,详情可见该网页
这里顺便说几个学习C++不错的网址,查询起来很方便
https://en.cppreference.com/w/
https://learncpp-cn.github.io/
https://cplusplus.com/
https://www.tutorialspoint.com/index.htm
https://awesome-cpp.readthedocs.io/en/latest/README.html#
https://stackoverflow.org.cn/标准库对输入参数binary_op的解释是:
将以未指定顺序应用于解引用输入迭代器结果、其他 binary_op 结果及 init 上的二元函数对象 (FunctionObject) 。
我们再看下类似的std::transform,详情可见该网页
可能实现的版本有
template<class InputIt1, class InputIt2,
         class OutputIt, class BinaryOperation>
OutputIt transform(InputIt1 first1, InputIt1 last1, InputIt2 first2,
                   OutputIt d_first, BinaryOperation binary_op)
{
    while (first1 != last1) {
      *d_first++ = binary_op(*first1++, *first2++);
    }
    return d_first;
}在这个程序中我们看到输入的参数可以传的参数可以函数指针,也可以是仿函数,而且这个仿函数需要传两个参数,按要求送达至返回值中
但按照C++的风格更喜欢是仿函数了
所以我们经常能看到类似这样的函数使用模式
#include #include #include int main () {   bool foo[] = {true,true,false,false};   bool bar[] = {true,false,true,false};   bool result;   std::transform (foo, foo+4, bar, result, std::logical_or());   std::cout
页: [1]
查看完整版本: Binary &Op是什么