C++函数对象-运算符函数对象 - 旧式绑定器与适配器 - 从成员函数指针创建包装器,能以一个对象引用调用 (std::mem_fun_ref)

任何定义了函数调用操作符的对象都是函数对象。C++ 支持创建、操作新的函数对象,同时也提供了许多内置的函数对象。

旧式绑定器与适配器

早期提供功能支持的几个工具在 C++11 中弃用,并于 C++17 中移除(旧否定器于 C++17 中弃用并于 C++20 中移除):

函数适配器

从成员函数指针创建包装器,能以一个对象引用调用

复制代码
std::mem_fun_ref

|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----|-------------------------|
| template< class Res, class T > std::mem_fun_ref_t<Res,T> mem_fun_ref( Res (T::*f)() ); | (1) | (C++11 中弃用) (C++17 中移除) |
| template< class Res, class T > std::const_mem_fun_ref_t<Res,T> mem_fun_ref( Res (T::*f)() const ); | (1) | (C++11 中弃用) (C++17 中移除) |
| template< class Res, class T, class Arg > std::mem_fun1_ref_t<Res,T,Arg> mem_fun_ref( Res (T::*f)(Arg) ); | (2) | (C++11 中弃用) (C++17 中移除) |
| template< class Res, class T, class Arg > std::const_mem_fun1_ref_t<Res,T,Arg> mem_fun_ref( Res (T::*f)(Arg) const ); | (2) | (C++11 中弃用) (C++17 中移除) |

创建成员函数包装对象,从模板实参推导类型。包装对象期待到 T 类型的引用作为其 operator() 的首个参数。

  1. 等效地调用 std::mem_fun_ref_t<S,T>(f) 或 std::const_mem_fun_ref_t<S,T>(f) 。

  2. 等效地调用 std::mem_fun1_ref_t<S,T>(f) 或 std::const_mem_fun1_ref_t<S,T>(f) 。

此函数与相关类型于 C++11 弃用并于 C++17 移除,为了让位给更通用的 std::mem_fn 与 std::bind ,它们都从成员函数创建可调用类型的兼容适配器的函数对象。

参数

|---|---|-----------------|
| f | - | 指向要创建包装的成员函数的指针 |

返回值

包装 f 的函数对象。

异常

可能会抛出由实现定义的异常。

注解

std::mem_fun 与 std::mem_fun_ref 的区别是前者产生的函数包装期待指向对象指针,而后者------期待引用。

示例

std::mem_fun_ref 绑定 std::string 的成员函数 size()

指向零元或一元成员函数指针的包装器,可以一个对象引用调用

复制代码
std::mem_fun_ref_t, 
std::mem_fun1_ref_t, 
std::const_mem_fun_ref_t, 
std::const_mem_fun1_ref_t

|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----|-------------------------|
| template< class S, class T > class mem_fun_ref_t : public unary_function<T,S> { public: explicit mem_fun_ref_t(S (T::*p)()); S operator()(T& p) const; }; | (1) | (C++11 中弃用) (C++17 中移除) |
| template< class S, class T > class const_mem_fun_ref_t : public unary_function<T,S> { public: explicit const_mem_fun_ref_t(S (T::*p)() const); S operator()(const T& p) const; }; | (2) | (C++11 中弃用) (C++17 中移除) |
| template< class S, class T, class A > class mem_fun1_ref_t : public binary_function<T,A,S> { public: explicit mem_fun1_ref_t(S (T::*p)(A)); S operator()(T& p, A x) const; }; | (3) | (C++11 中弃用) (C++17 中移除) |
| template< class S, class T, class A > class const_mem_fun1_ref_t : public binary_function<T,A,S> { public: explicit const_mem_fun1_ref_t(S (T::*p)(A) const); S operator()(const T& p, A x) const; }; | (4) | (C++11 中弃用) (C++17 中移除) |

围绕成员函数指针的包装器。将要调用其成员函数的类实例作为引用传递给 operator()

  1. 包装无参数的非 const 成员函数。

  2. 包装无参数的 const 成员函数。

  3. 包装有单参数的非 const 成员函数。

  4. 包装有单参数的 const 成员函数。

调用示例

复制代码
#include <iostream>
#include <functional>
#include <algorithm>
#include <cmath>
#include <iterator>

struct Cell
{
    int x;
    int y;

    Cell() = default;
    Cell(int a, int b): x(a), y(b) {}
    Cell(const Cell &cell)
    {
        x = cell.x;
        y = cell.y;
    }

    Cell &operator+(const Cell &cell)
    {
        x += cell.x;
        y += cell.y;
        return *this;
    }

    Cell &operator+=(const Cell &cell)
    {
        x += cell.x;
        y += cell.y;
        return *this;
    }

    Cell &operator*=(int n)
    {
        x *= n;
        y *= n;
        return *this;
    }

    Cell &operator++()
    {
        x += 1;
        y += 1;
        return *this;
    }

    friend Cell operator +(const Cell &cell1, const Cell &cell2)
    {
        Cell cell = cell1;
        cell += cell2;
        return cell;
    }

    friend Cell operator *(const Cell &cell1, const Cell &cell2)
    {
        Cell cell = {cell1.x * cell2.x, cell1.y * cell2.y};
        return cell;
    }

    friend Cell operator /(const Cell &cell1, const Cell &cell2)
    {
        Cell cell = {cell1.x / cell2.x, cell1.y / cell2.y};
        return cell;
    }

    friend Cell operator %(const Cell &cell1, const Cell &cell2)
    {
        Cell cell = {cell1.x % cell2.x, cell1.y % cell2.y};
        return cell;
    }

    friend bool operator ==(const Cell &cell1, const Cell &cell2)
    {
        return cell1.x == cell2.x && cell1.y == cell2.y;
    }

    friend bool operator !=(const Cell &cell1, const Cell &cell2)
    {
        return cell1.x != cell2.x && cell1.y != cell2.y;
    }

    friend bool operator <(const Cell &cell1, const Cell &cell2)
    {
        if (cell1.x == cell2.x)
        {
            return cell1.y < cell2.y;
        }
        else
        {
            return cell1.x < cell2.x;
        }
    }

    friend bool operator >(const Cell &cell1, const Cell &cell2)
    {
        if (cell1.x == cell2.x)
        {
            return cell1.y > cell2.y;
        }
        else
        {
            return cell1.x > cell2.x;
        }
    }

    friend bool operator &&(const Cell &cell1, const Cell &cell2)
    {
        return cell1.x && cell2.x && cell1.y && cell2.y;
    }

    friend bool operator ||(const Cell &cell1, const Cell &cell2)
    {
        return cell1.x || cell2.x || cell1.y || cell2.y;
    }

    friend bool operator !(const Cell &cell)
    {
        return !(cell.x && cell.x);
    }

    friend Cell operator &(const Cell &cell1, const Cell &cell2)
    {
        Cell cell = {cell1.x & cell2.x, cell1.y & cell2.y};
        return cell;
    }

    friend Cell operator |(const Cell &cell1, const Cell &cell2)
    {
        Cell cell = {cell1.x | cell2.x, cell1.y | cell2.y};
        return cell;
    }

    friend Cell operator ^(const Cell &cell1, const Cell &cell2)
    {
        Cell cell = {cell1.x ^ cell2.x, cell1.y ^ cell2.y};
        return cell;
    }

    Cell copy_mem_fun_ref()
    {
        return *this;
    }

    Cell copy_const_mem_fun_ref()
    {
        return *this;
    }

    void print() const
    {
        std::cout << "{" << x << "," << y << "} ";
    }
};

std::ostream &operator<<(std::ostream &os, const Cell &cell)
{
    os << "{" << cell.x << "," << cell.y << "}";
    return os;
}

struct mem_fun_ref_t : std::mem_fun_ref_t<Cell, Cell>
{
    Cell operator()(const Cell cell) const
    {
        return cell;
    }
};

struct const_mem_fun_ref_t : std::const_mem_fun_ref_t<Cell, Cell>
{
    Cell operator()(const Cell cell) const
    {
        return cell;
    }
};

struct mem_fun1_ref_t : std::mem_fun1_ref_t<Cell, Cell, Cell>
{
    Cell operator()(const Cell cell) const
    {
        return cell;
    }
};

struct const_mem_fun1_ref_t : std::const_mem_fun1_ref_t<Cell, Cell, Cell>
{
    Cell operator()(const Cell cell) const
    {
        return cell;
    }
};

int main()
{
    std::vector<Cell> vector1{{101, 101}, {102, 102}, {103, 103}, {104, 104}, {105, 105}};
    std::vector<Cell> result1(vector1.size());
    std::transform(vector1.begin(), vector1.end(), result1.begin(),
                   std::mem_fun_ref(&Cell::copy_mem_fun_ref));

    std::cout << "data:     ";
    std::for_each(vector1.begin(), vector1.end(), std::mem_fun_ref(&Cell::print));
    std::cout << std::endl;
    std::cout << "result:   ";
    std::copy(result1.begin(), result1.end(), std::ostream_iterator<Cell>(std::cout, " "));
    std::cout << std::endl;
    std::cout << std::endl;

    return 0;
}

输出

复制代码
data:     {101,101} {102,102} {103,103} {104,104} {105,105}
result:   {101,101} {102,102} {103,103} {104,104} {105,105}
相关推荐
在路上看风景3 小时前
19. 成员初始化列表和初始化对象
c++
zmzb01033 小时前
C++课后习题训练记录Day98
开发语言·c++
念风零壹4 小时前
C++ 内存避坑指南:如何用移动语义和智能指针解决“深拷贝”与“内存泄漏”
c++
孞㐑¥5 小时前
算法——BFS
开发语言·c++·经验分享·笔记·算法
MZ_ZXD0016 小时前
springboot旅游信息管理系统-计算机毕业设计源码21675
java·c++·vue.js·spring boot·python·django·php
A星空1237 小时前
一、Linux嵌入式的I2C驱动开发
linux·c++·驱动开发·i2c
凡人叶枫8 小时前
C++中智能指针详解(Linux实战版)| 彻底解决内存泄漏,新手也能吃透
java·linux·c语言·开发语言·c++·嵌入式开发
会叫的恐龙8 小时前
C++ 核心知识点汇总(第六日)(字符串)
c++·算法·字符串
小糯米6018 小时前
C++顺序表和vector
开发语言·c++·算法
独望漫天星辰8 小时前
C++ 多态深度解析:从语法规则到底层实现(附实战验证代码)
开发语言·c++