std::mem_fn

1. 不支持的场景

1.1 不支持全局函数

1.2 不支持类protected访问权限的成员(函数或数据)

1.3 不支持类private访问权限的成员(函数或数据)

2. 支持的场景

2.1 传入类对象

2.2 传入引用对象

2.3 传入右值

2.4 传入对象指针

2.5 传入智能指针std::shared_ptr

2.6 传入智能指针std::unique_ptr

2.7 传入派生类对象

2.8 带参数的成员函数

3. 场景示例代码

#include <functional>
#include <iostream>

int Add(int a, int b)
{
    return a + b;
}

class Age
{
public:
    Age(int default = 12) : m_age(default)
    {}

    bool compare(const Age& t) const
    {
        return m_age < t.m_age;
    }

    void print() const
    {
        std::cout << m_age << ' ';
    }

    int m_age;

protected:
    void add(int n)
    {
        m_age += n;
    }

private:
    void sub(int m)
    {
        m_age -= m;
    }
};

class DerivedAge : public Age
{
public:
    DerivedAge(int default = 22) : Age(default)
    {}
};

int main(int argc, char* argv[])
{
    // 0.不支持的示例
    {
        // 1.不支持全局函数
        // auto globalFunc = std::mem_fn(Add); // ERROR: 语法无法通过
        // 2.不支持类protected访问权限的函数
        // auto addFunc = std::mem_fn(&Age::add); // ERROR: 语法无法通过
        // 3.不支持类private访问权限的函数
        // auto subFunc = std::mem_fn(&Age::sub); // ERROR: 语法无法通过
    }
    // 1.成员函数示例
    {
        auto memFunc = std::mem_fn(&Age::print);

        // 方式一:传入类对象
        Age obja{ 18 };
        memFunc(obja);

        Age& refObj = obja;
        refObj.m_age = 28;
        // 方式二:传入引用对象
        memFunc(refObj);

        // 方式三:传入右值
        Age objb{ 38 };
        memFunc(std::move(objb));

        // 方式四:传入对象指针
        Age objc{ 48 };
        memFunc(&objc);

        // 方式五:传入智能指针std::shared_ptr
        std::shared_ptr<Age> pAge1 = std::make_shared<Age>(58);
        memFunc(pAge1);

        // 方式六:传入智能指针std::unique_ptr
        std::unique_ptr<Age> pAge2 = std::make_unique<Age>(68);
        memFunc(pAge2);

        // 方式七:传入派生类对象
        DerivedAge aged{ 78 };
        memFunc(aged);
        
        // 方式八:带参数成员函数
        auto memFuncWithParams = std::mem_fn(&Age::compare);
        std::cout << memFuncWithParams(Age{ 25 }, Age{ 35 }) << std::endl;
    }

    std::cout << std::endl;

    // 2.成员变量示例
    {
        auto memData = std::mem_fn(&Age::m_age);

        // 方式一:传入类对象
        Age obja{ 19 };
        std::cout << memData(obja) << ' ';

        Age& refObj = obja;
        refObj.m_age = 29;
        // 方式二:传入引用对象
        std::cout << memData(refObj) << ' ';

        // 方式三:传入右值
        Age objb{ 39 };
        std::cout << memData(std::move(objb)) << ' ';

        // 方式四:传入对象指针
        Age objc{ 49 };
        std::cout << memData(&objc) << ' ';

        // 方式五:传入智能指针std::shared_ptr
        std::shared_ptr<Age> pAge1 = std::make_shared<Age>(59);
        std::cout << memData(pAge1) << ' ';

        // 方式六:传入智能指针std::unique_ptr
        std::unique_ptr<Age> pAge2 = std::make_unique<Age>(69);
        std::cout << memData(pAge2) << ' ';

        // 方式七:传入派生类对象
        DerivedAge aged{ 79 };
        std::cout << memData(aged) << ' ';
    }

    return 0;
}

/* run output:
18 28 38 48 58 68 78 1
19 29 39 49 59 69 79
*/

4. 应用示例

4.1 以前写法

4.2 使用std::mem_fn写法

4.3 示例代码

#include <functional>
#include <iostream>
#include <algorithm>
#include <vector>

class Age
{
public:
    Age(int v) : m_age(v)
    {}

    bool compare(const Age& t) const
    {
        return m_age < t.m_age;
    }

    void print() const
    {
        std::cout << m_age << ' ';
    }

    int m_age;
};

bool compare(const Age& t1, const Age& t2)
{
    return t1.compare(t2);
}

int main(int argc, char* argv[])
{
    // 以前写法
    {
        std::vector<Age> ages{ 1, 7, 19, 27, 39, 16, 13, 18 };
        std::sort(ages.begin(), ages.end(), [&](const Age& objA, const Age& objB) {
            return compare(objA, objB);
            });
        for (auto item : ages)
        {
            item.print();
        }
        std::cout << std::endl;
    }
    // 利用std::mem_fn写法
    {
        std::vector<Age> ages{ 100, 70, 290, 170, 390, 160, 300, 180 };
        std::sort(ages.begin(), ages.end(), std::mem_fn(&Age::compare));
        std::for_each(ages.begin(), ages.end(), std::mem_fn(&Age::print));
        std::cout << std::endl;
    }

    return 0;
}

/* run output:
1 7 13 16 18 19 27 39
70 100 160 170 180 290 300 390
*/

原文地址:http://www.cnblogs.com/Braveliu/p/16875374.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性