首页 > 其他分享 >std::function

std::function

时间:2023-10-13 15:11:06浏览次数:36  
标签:std function int include Minus 模板

参考资料


• cplusplus.comhttp://www.cplusplus.com/reference/functional/function/        https://www.cnblogs.com/heartchord/p/5017071.html

• cppreference.comhttp://en.cppreference.com/w/cpp/utility/functional/function

std::function简介


• 类模板声明

复制代码
// MS C++ 2013
template<class _Fty> class function;
template<class _Fty> class function : public _Get_function_impl<_Fty>::type { ... }

// GCC 4.8.2
template<typename _Signature>                  class function;
template<typename _Res, typename... _ArgTypes> class function<_Res(_ArgTypes...)>
    : public _Maybe_unary_or_binary_function<_Res, _ArgTypes...>, private _Function_base { ... }

// cplusplus.com
template <class T> function; // undefined template <class Ret, class... Args> class function<Ret(Args...)>;
复制代码

• 类模板说明

       std::function是一个函数包装器模板,最早来自boost库,对应其boost::function函数包装器。在c++0x11中,将boost::function纳入标准库中。该函数包装器模板能包装任何类型的可调用元素(callable element),例如普通函数和函数对象。包装器对象可以进行拷贝,并且包装器类型仅仅只依赖于其用特征(call signature),而不依赖于可调用元素自身的类型。

       一个std::function类型对象实例可以包装下列这几种可调用元素类型:函数、函数指针、类成员函数指针或任意类型的函数对象(例如定义了operator()操作并拥有函数闭包)。std::function对象可被拷贝和转移,并且可以使用指定的调用特征来直接调用目标元素。当std::function对象未包裹任何实际的可调用元素,调用该std::function对象将抛出std::bad_function_call异常

• 模板参数说明

       以cplusplus.com中描述的原型说明:

       T      : 通用类型,但实际通用类型模板并没有被定义,只有当T的类型为形如Ret(Args...)的函数类型才能工作。

       Ret   : 调用函数返回值的类型。

       Args : 函数参数类型。

std::function详解


• 包装普通函数

复制代码
#include <iostream>
#include <functional>
using namespace std;

int g_Minus(int i, int j)
{
    return i - j;
}

int main()
{
    function<int(int, int)> f = g_Minus;
    cout << f(1, 2) << endl;                                            // -1
    return 1;
}
复制代码

• 包装模板函数

复制代码
#include <iostream>
#include <functional>
using namespace std;

template <class T>
T g_Minus(T i, T j)
{
    return i - j;
}

int main()
{
    function<int(int, int)> f = g_Minus<int>;
    cout << f(1, 2) << endl;                                            // -1
    return 1;
}
复制代码

• 包装lambda表达式

复制代码
#include <iostream>
#include <functional>
using namespace std;

auto g_Minus = [](int i, int j){ return i - j; };

int main()
{
    function<int(int, int)> f = g_Minus;
    cout << f(1, 2) << endl;                                            // -1
    return 1;
}
复制代码

• 包装函数对象

       非模板类型:

复制代码
#include <iostream>
#include <functional>
using namespace std;

struct Minus
{
    int operator() (int i, int j)
    {
        return i - j;
    }
};

int main()
{
    function<int(int, int)> f = Minus();
    cout << f(1, 2) << endl;                                            // -1
    return 1;
}
复制代码

       模板类型:

复制代码
#include <iostream>
#include <functional>
using namespace std;

template <class T>
struct Minus
{
    T operator() (T i, T j)
    {
        return i - j;
    }
};

int main()
{
    function<int(int, int)> f = Minus<int>();
    cout << f(1, 2) << endl;                                            // -1
    return 1;
}
复制代码

• 包装类静态成员函数

       非模板类型:

复制代码
#include <iostream>
#include <functional>
using namespace std;

class Math
{
public:
    static int Minus(int i, int j)
    {
        return i - j;
    }
};

int main()
{
    function<int(int, int)> f = &Math::Minus;
    cout << f(1, 2) << endl;                                            // -1
    return 1;
}
复制代码

       模板类型:

复制代码
#include <iostream>
#include <functional>
using namespace std;

class Math
{
public:
    template <class T>
    static T Minus(T i, T j)
    {
        return i - j;
    }
};

int main()
{
    function<int(int, int)> f = &Math::Minus<int>;
    cout << f(1, 2) << endl;                                            // -1
    return 1;
}
复制代码

• 包装类对象成员函数

       非模板类型:

复制代码
#include <iostream>
#include <functional>
using namespace std;

class Math
{
public:
    int Minus(int i, int j)
    {
        return i - j;
    }
};

int main()
{
    Math m;
    function<int(int, int)> f = bind(&Math::Minus, &m, placeholders::_1, placeholders::_2);
    cout << f(1, 2) << endl;                                            // -1
    return 1;
}
复制代码

       模板类型:

复制代码
#include <iostream>
#include <functional>
using namespace std;

class Math
{
public:
    template <class T>
    T Minus(T i, T j)
    {
        return i - j;
    }
};

int main()
{
    Math m;
    function<int(int, int)> f = bind(&Math::Minus<int>, &m, placeholders::_1, placeholders::_2);
    cout << f(1, 2) << endl;                                            // -1
    return 1;
}
复制代码

 

标签:std,function,int,include,Minus,模板
From: https://www.cnblogs.com/tomato-haha/p/17762159.html

相关文章

  • FastDFS+Nginx - 本地搭建文件服务器同时实现在外远程访问「端口映射」 转载
    前言FastDFS是一个开源的轻量级分布式文件系统,它对文件进行管理,功能包括:文件存储、文件同步、文件访问(文件上传、文件下载)等,解决了大容量存储和负载均衡的问题。特别适合以文件为载体的在线服务,如相册网站、视频网站等等。FastDFS为互联网量身定制,充分考虑了冗余备份、负载均衡......
  • Vue报错Syntax Error:TypeError: this.getOptions is not a function的解决方法~
    前几天在vue运行项目过程中报错了,这个方法是关于Vue报错SyntaxError:TypeError:this.getOptionsisnotafunction的解决方法(1)报错一(2)报错二~1.1问题分析首先,检查代码,并没有什么错误的地方;其次,涉及到这个问题,可能就是版本原因了,安装的sass-loader版本太高,卸载安装低......
  • 启动vue项目报错——ERROR Error loading vue.config.js: ERROR TypeError: defineCon
    问题描述在我引入echarts模块之前是ok的,引入之后就启动失败了;问题解决一般情况下,都是该项目的版本与本机cmd里面的版本不对应导致的;只需要使用这个命令npmupgrade,更新版本,一直yes下去,就能够解决这个问题啦!......
  • fatal: sha1 file '<stdout>' write error: Broken pipe
    解决使用GitLFS在官网 https://git-lfs.github.com/ 下载git-lfs-windows-v2.8.0.exe并安装。新开一个bash命令行输入gitlfsinstall安装跟踪你要push的大文件gitlfstrack"*.h5",这时会生成一个.gitattributes文件。这里很重要,一定要先将上一步生成的.gitattributes......
  • FastDFS+Nginx,轻轻松松搭建一个本地文件服务器
    前言1.本地搭建FastDFS文件系统2.局域网测试访问FastDFS3.安装cpolar内网穿透4.配置公网访问地址5.固定公网地址6.测试访问固定二级子域名前言FastDFS是一个开源的轻量级分布式文件系统,它对文件进行管理,功能包括:文件存储、文件同步、文件访问(文件上传、文件下载)等,解决......
  • CentOS 7.9 FastDFS 设置开机自启动
    CentOS7.9FastDFS设置开机自启动  一、前言关于 FastDFS服务的启动、停止、重启相关脚本,可以参考如下博客:https://www.cnblogs.com/miracle-luna/p/17750542.html本文主要讲解如何使用systemctl系统命令,进行启动、停止、重启、查看FastDFS状态等操作。 二、......
  • P7710 [Ynoi2077] stdmxeypz 题解
    P7710[Ynoi2077]stdmxeypz题解我的第一道Ynoi题,体验感不高,调了大半天,最后发现有个地方\(B_1\)写成\(B_2\)了。分析树上问题,明显是要转到树下的,所以DFS序是一定要求的。有关树上距离,所以\(deep\)数组也是一定要求的。所以我们现在把问题转化成了:给你三个序列\(......
  • 疑似std::regex_search正则匹配,导致堆栈错误
    一个很奇怪的问题,当我_beginthreadex/CreateThread创建线程,使用std::regex_search匹配时,程序会崩溃,堆栈如下:ntdll.dll!RtlReportCriticalFailure()未知ntdll.dll!RtlpHeapHandleError()未知ntdll.dll!RtlpHpHeapHandleError()未知n......
  • npm install 报 cb.apply is not a function 错误
    npminstall报cb.applyisnotafunction错误1、问题来源.当我执行npminstall命令时,出现cb.applyisnotafunction错误!.由此可知,可能是npm和node版本不匹配。解决方案更换版本.node与npm版本对应表.参考资料node版本对应的npm版本表.解决npm......
  • [Microsoft Azure] 创建你的第一个Azure Functions 应用
    随着云计算和Serverless架构的普及,微服务变得越来越受欢迎。AzureFunctions是MicrosoftAzure提供的一种Serverless服务,可以让你在Azure上快速部署和运行代码,而无需管理底层服务器。在这篇文章中,我们将带你创建一个简单的AzureFunctions应用,并了解其基本概念和工作原理。一......