std::bind

利用std::bind可以改变可调用对象的参数数量顺序
std::placeholders::_n为占位符,代表新的可调用对象的第n个参数。

auto g = bind(f, a, b, std::placeholders:_2, c, std::placeholders:_1);

假定f是一个可调用对象,它有5个参数,使用std::bind绑定后,生成新的可调用对象g,它有两个参数,分别是std::placeholders::_1和std::placeholders::_2。
g的第一个参数为_1,第二个参数为_2,对应到f上分别是f的第5个参数和第三个参数。

示例:

#include <iostream>
#include <functional>

void printDate(int day, int month, int year)
{
    std::cout << year << "/" << month << "/" << day << std::endl;
}


int main()
{
// 使用示例
    auto printDateNew = std::bind(printDate, std::placeholders::_2, std::placeholders::_1, 2024);
    
    printDateNew(11, 12);

    return 0;
    
}

© 版权声明
THE END
如果内容对您有所帮助,就支持一下吧!
点赞0 分享
花狼狼狼的头像 - 宋马
评论 抢沙发

请登录后发表评论

    暂无评论内容