92 CPP11神器之包装器function

文章详细介绍了C++中的std::function模板类,它作为一个通用的可调用对象包装器,可以封装各种类型的函数、成员函数、仿函数以及lambda表达式。通过std::function,可以以统一的方式处理这些不同类型的可调用对象,并且提供了bool运算符重载来检查对象是否已包装。文中给出了多个示例,包括普通函数、静态成员函数、仿函数、lambda表达式以及通过类转换为函数指针的例子,展示了std::function的使用方法。

std::function模板类是一个通用的可调用对象的包装器,用简单的、统一的方式处理可调用对象。

template

class function......

_Fty是可调用对象的类型,格式:返回列表(参数列表)。

包含头文件 #include

注意:

1 重载了bool运算符,用于判断是否包装了可调用对象。

2 如果std::function对象未包装可调用对象,使用std::function对象将抛出异常 std::bad_function_call

#include <iostream>
#include <functional>
using namespace std;
//普通函数
void show(int a, const string &b)
{
    cout << a << b << endl;
}

//类中有静态成员函数
struct AA
{
    static void show(int a, const string &b)
    {
        cout << a << b << endl;
    }
};

//仿函数
struct BB
{
    void operator()(int a, const string &b)
    {
        cout << a << b << endl;
    }
};
//类中有普通函数
struct CC
{
    void show(int a, const string &b)
    {
        cout << a << b << endl;
    }
};

//可以被转换为普通函数指针的类
struct DD
{
    using Fun = void (*)(int, const string &);
    operator Fun()
  
35 0 C:\Program Files (x86)\Dev-Cpp\MinGW64\lib\gcc\x86_64-w64-mingw32\4.9.2\include\c++\array In file included from C:/Program Files (x86)/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.9.2/include/c++/array 2 D:\范铭博编程文件\特殊\牛逼的东西.cpp from D:\范铭博编程文件\特殊\牛逼的东西.cpp 32 2 C:\Program Files (x86)\Dev-Cpp\MinGW64\lib\gcc\x86_64-w64-mingw32\4.9.2\include\c++\bits\c++0x_warning.h [Error] #error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options. 8 1 D:\范铭博编程文件\特殊\牛逼的东西.cpp [Error] 'constexpr' does not name a type 8 1 D:\范铭博编程文件\特殊\牛逼的东西.cpp [Note] C++11 'constexpr' only available with -std=c++11 or -std=gnu++11 D:\范铭博编程文件\特殊\牛逼的东西.cpp In function 'int main()': 26 5 D:\范铭博编程文件\特殊\牛逼的东西.cpp [Error] 'constexpr' was not declared in this scope 30 14 D:\范铭博编程文件\特殊\牛逼的东西.cpp [Error] 'function' in namespace 'std' does not name a template type 31 30 D:\范铭博编程文件\特殊\牛逼的东西.cpp [Error] expected ')' before '<' token D:\范铭博编程文件\特殊\牛逼的东西.cpp In destructor 'main()::Printer::~Printer()': 32 29 D:\范铭博编程文件\特殊\牛逼的东西.cpp [Error] 'action' was not declared in this scope D:\范铭博编程文件\特殊\牛逼的东西.cpp In lambda function: 39 35 D:\范铭博编程文件\特殊\牛逼的东西.cpp [Warning] extended initializer lists only available with -std=c++11 or -std=gnu++11 D:\范铭博编程文件\特殊\牛逼的东西.cpp In function 'int main()': 42 5 D:\范铭博编程文件\特殊\牛逼的东西.cpp [Warning] lambda expressions only available with -std=c++11 or -std=gnu++11 42 6 D:\范铭博编程文件\特殊\牛逼的东西.cpp [Error] no matching function for call to 'main()::Printer::Printer(main()::<lambda()>)' 42 6 D:\范铭博编程文件\特殊\牛逼的东西.cpp [Note] candidates are: 29 12 D:\范铭博编程文件\特殊\牛逼的东西.cpp [Note] main()::Printer::Printer() 29 12 D:\范铭博编程文件\特殊\牛逼的东西.cpp [Note] candidate expects 0 arguments, 1 provided 29 12 D:\范铭博编程文件\特殊\牛逼的东西.cpp [Note] main()::Printer::Printer(const main()::Printer&) 29 12 D:\范铭博编程文件\特殊\牛逼的东西.cpp [Note] no known conversion for argument 1 from 'main()::<lambda()>' to 'const main()::Printer&' 45 7 D:\范铭博编程文件\特殊\牛逼的东西.cpp [Error] 'struct main()::Printer' has no member named 'action' 48 16 D:\范铭博编程文件\特殊\牛逼的东西.cpp [Warning] variadic templates only available with -std=c++11 or -std=gnu++11 48 16 D:\范铭博编程文件\特殊\牛逼的东西.cpp [Error] expansion pattern 'int' contains no argument packs D:\范铭博编程文件\特殊\牛逼的东西.cpp In lambda function: 49 9 D:\范铭博编程文件\特殊\牛逼的东西.cpp [Error] 'array' is not a member of 'std' 50 19 D:\范铭博编程文件\特殊\牛逼的东西.cpp [Error] 'arr' was not declared in this scope 52 19 D:\范铭博编程文件\特殊\牛逼的东西.cpp [Error] 'x' does not name a type 53 9 D:\范铭博编程文件\特殊\牛逼的东西.cpp [Error] expected ';' before 'std' 54 5 D:\范铭博编程文件\特殊\牛逼的东西.cpp [Error] expected primary-expression before '}' token 54 5 D:\范铭博编程文件\特殊\牛逼的东西.cpp [Error] expected ')' before '}' token 54 5 D:\范铭博编程文件\特殊\牛逼的东西.cpp [Error] expected primary-expression before '}' token D:\范铭博编程文件\特殊\牛逼的东西.cpp In function 'int main()': 54 5 D:\范铭博编程文件\特殊\牛逼的东西.cpp [Warning] lambda expressions only available with -std=c++11 or -std=gnu++11 54 20 D:\范铭博编程文件\特殊\牛逼的东西.cpp [Error] no match for call to '(main()::<lambda()>) (int, int, int, int, int)' 48 6 D:\范铭博编程文件\特殊\牛逼的东西.cpp [Note] candidates are: 54 20 D:\范铭博编程文件\特殊\牛逼的东西.cpp [Note] void (*)() <conversion> 54 20 D:\范铭博编程文件\特殊\牛逼的东西.cpp [Note] candidate expects 1 argument, 6 provided 48 17 D:\范铭博编程文件\特殊\牛逼的东西.cpp [Note] main()::<lambda()> 48 17 D:\范铭博编程文件\特殊\牛逼的东西.cpp [Note] candidate expects 0 arguments, 5 provided
10-05
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值