C++11 – std::function简要介绍以及可包装函数的几种形式总结
本文作者:StubbornHuang
版权声明:本文为站长原创文章,如果转载请注明原文链接!
原文标题:C++11 – std::function简要介绍以及可包装函数的几种形式总结
原文链接:https://www.stubbornhuang.com/916/
发布于:2020年09月24日 16:07:25
修改于:2021年07月15日 21:25:54

1 std::function
函数模板形式:
template <class T>函数;//未定义
模板<class Ret,class ... Args> class function <Ret(Args ...)>;
1.1 函数模板说明
std::function是一个函数包装器模板,该函数包装器模板可以包装任意可以调用的元素,其包装器的类型只依赖于其调用特征,而不依赖于可调用元素自身的类型。
std::function可包装下列可调用元素类型:
- 函数
- 函数指针
- 类成员函数
- 任意类型的函数可调用对象(比如重载了operator()操作符并且拥有函数闭包的函数体对象)
- ...
std::function对象可被拷贝和转移,并且可以使用指定的调用特征来直接调用目标元素。
当std::function对象未包裹任何实际的可调用元素,调用该std::function对象将抛出std::bad_function_call异常。
1.2 模板参数说明
- T : 通用类型,但实际通用类型模板并没有被定义,只有当T的类型为形如Ret(Args...)的函数类型才能工作;
- Ret :调用函数返回值的类型;
- Args:函数参数类型,对于指向成员函数的指针,第一个参数应该是指向该成员函数的引用或者对象。
2 std::function可包装的几种函数形式总结
2.1 包装普通函数和静态函数
2.1.1 非模板类型
代码示例:
#include <iostream>
#include <functional>
#include <memory>
using namespace std;
int Add(int a, int b)
{
std::cout << "普通函数被调用,结果为:" << a + b << std::endl;
return a + b;
}
static int StaticAdd(int a, int b)
{
std::cout << "静态函数被调用,结果为:" << a + b << std::endl;
return a + b;
}
int main()
{
// 1 普通函数
std::function<int(int, int)> addFunc1 = Add;
addFunc1(1, 2);
// 2 普通函数指针
std::function<int(int, int)> addFunc2 = &Add;
addFunc2(3, 4);
// 3 静态函数
std::function<int(int, int)> staticAddFunc1 = StaticAdd;
staticAddFunc1(5, 6);
// 4 静态函数指针
std::function<int(int, int)> staticAddFunc2 = &StaticAdd;
staticAddFunc2(7, 8);
getchar();
return 0;
}
2.1.2 模板类型
代码示例:
#include <iostream>
#include <functional>
#include <memory>
using namespace std;
template <class T>
T Add(T a, T b)
{
std::cout << "普通模板函数被调用,结果为:" << a + b << std::endl;
return a + b;
}
template <class T>
static T StaticAdd(T a, T b)
{
std::cout << "静态模板函数被调用,结果为:" << a + b << std::endl;
return a + b;
}
int main()
{
// 1 普通函数
std::function<int(int, int)> addFunc1 = Add<int>;
addFunc1(1, 2);
// 2 普通函数指针
std::function<int(int, int)> addFunc2 = &Add<int>;
addFunc2(3, 4);
// 3 静态函数
std::function<int(int, int)> staticAddFunc1 = StaticAdd<int>;
staticAddFunc1(5, 6);
// 4 静态函数指针
std::function<int(int, int)> staticAddFunc2 = &StaticAdd<int>;
staticAddFunc2(7, 8);
getchar();
return 0;
}
2.2 包装类成员函数和类静态函数
2.2.1 非模板类型
代码示例:
#include <iostream>
#include <functional>
#include <memory>
using namespace std;
class A
{
public:
typedef std::shared_ptr<A> ptr;
A()
{
};
virtual~A()
{
};
public:
int A_Add_Public(int a, int b)
{
std::cout << "类公有成员函数被调用,结果为:" << a + b << std::endl;
return a + b;
}
static int A_Add_Static(int a, int b)
{
std::cout << "类静态函数被调用,结果为:" << a + b << std::endl;
return a + b;
}
public:
int m_Value = 20;
};
int main()
{
// 1 类公有成员函数
A m_a;
std::function<int(int, int)> addFunc1 = std::bind(&A::A_Add_Public, &m_a, std::placeholders::_1, std::placeholders::_2);
addFunc1(1, 2);
// 2 类静态函数
std::function<int(int, int)> addFunc2 = &A::A_Add_Static;
addFunc2(1, 2);
getchar();
return 0;
}
2.2.2 模板类型
代码示例:
#include <iostream>
#include <functional>
#include <memory>
using namespace std;
class A
{
public:
typedef std::shared_ptr<A> ptr;
A()
{
};
virtual~A()
{
};
public:
template <class T>
T A_Add_Public(T a, T b)
{
std::cout << "类公有模板成员函数被调用,结果为:" << a + b << std::endl;
return a + b;
}
template <class T>
static T A_Add_Static(T a, T b)
{
std::cout << "类静态模板函数被调用,结果为:" << a + b << std::endl;
return a + b;
}
public:
int m_Value = 20;
};
int main()
{
// 1 类公有成员函数
A m_a;
std::function<int(int, int)> addFunc1 = std::bind(&A::A_Add_Public<int>, &m_a, std::placeholders::_1, std::placeholders::_2);
addFunc1(1, 2);
// 2 类静态函数
std::function<int(int, int)> addFunc2 = &A::A_Add_Static<int>;
addFunc2(1, 2);
getchar();
return 0;
}
2.3 包装Lambda表达式
代码示例:
#include <iostream>
#include <functional>
#include <memory>
using namespace std;
int main()
{
// 1 lambda表达式
std::function<int(int, int)> addFunc1 = [](int a, int b) ->int {
std::cout << "lambda表达式被调用,结果为:" << a + b << std::endl;
return a + b;
};
addFunc1(1, 2);
getchar();
return 0;
}
2.4 包装类重载操作符()函数
2.4.1 非模板类型
代码示例:
#include <iostream>
#include <functional>
#include <memory>
using namespace std;
class A
{
public:
typedef std::shared_ptr<A> ptr;
A()
{
};
virtual~A()
{
};
public:
int operator()(int a, int b)
{
std::cout << "类重载操作符()函数被调用,结果为:" << a + b << std::endl;
return a + b;
}
public:
int m_Value = 20;
};
int main()
{
// 1 类重载操作符()函数
std::function<int(int, int)> addFunc1 = A();
addFunc1(1, 2);
getchar();
return 0;
}
2.4.2 模板类型
代码示例:
#include <iostream>
#include <functional>
#include <memory>
using namespace std;
template <class T>
class A
{
public:
typedef std::shared_ptr<A> ptr;
A()
{
};
virtual~A()
{
};
public:
T operator()(T a, T b)
{
std::cout << "类重载操作符()模板函数被调用,结果为:" << a + b << std::endl;
return a + b;
}
public:
int m_Value = 20;
};
int main()
{
// 1 类重载操作符()函数
std::function<int(int, int)> addFunc1 = A<int>();
addFunc1(1, 2);
getchar();
return 0;
}
2.5 包装类共有成员变量
代码示例:
#include <iostream>
#include <functional>
#include <memory>
using namespace std;
class A
{
public:
typedef std::shared_ptr<A> ptr;
A()
{
};
virtual~A()
{
};
public:
int Add()
{
return m_Value;
}
public:
int m_Value = 20;
};
int main()
{
// 1 类共有成员变量
std::function<int(A&)> A_Value = &A::m_Value;
A m_a;
std::cout << "m_a对象的成员m_Value的值为:" << A_Value(m_a) << std::endl;
getchar();
return 0;
}
当前分类随机文章推荐
- C++ - C++使用cuda api获取当前GPU显卡的总共的显存容量、已使用显存容量、剩余显存容量 阅读1350次,点赞1次
- C++ - 使用Websocket++编写客户端连接WebSocket服务器并进行通信 阅读2497次,点赞2次
- C++STL容器 - std::vector构造方式与分配值方式总结 阅读182次,点赞0次
- C++ - std::string替换字符串中所有指定的子字符串 阅读187次,点赞0次
- C++ – 字节数组byte[]或者unsigned char[]与float的相互转换 阅读391次,点赞0次
- C++ – 字节数组byte[]或者unsigned char[]与short的相互转换 阅读316次,点赞0次
- C++ - 字节数组byte[]或者unsigned char[]与long的相互转换 阅读280次,点赞0次
- C++ - Windows和Linux系统下获取当前可执行程序的绝对路径 阅读279次,点赞0次
- Windows - 使用类的成员函数作为Win32窗口消息回调处理函数WindowProc 阅读189次,点赞0次
- C++ - int转string方法总结 阅读4185次,点赞0次
全站随机文章推荐
- 神经网络 - 模型训练需注意的细节与超参数调优 阅读245次,点赞1次
- 资源分享 - Computer Graphics Principles and Practice (3rd edition) 英文高清PDF下载 阅读2148次,点赞0次
- 资源分享 - Fundamentals of Computer Graphics, Fifth Edition 英文高清PDF下载 阅读2163次,点赞0次
- 资源分享 - Essential Mathematics for Games and Interactive Applications(First Edition) 英文高清PDF下载 阅读1134次,点赞0次
- 资源分享 - Game Physics Engine Development- How to Build a Robust Commercial-Grade Physics Engine for your Game (Second Edition) 英文高清PDF下载 阅读1195次,点赞0次
- 资源分享 - Fundamentals of Computer Graphics, Fourth Edition高清英文PDF下载 阅读10492次,点赞8次
- WordPress - get_edit_post_link函数详解 阅读412次,点赞0次
- Duilib - CDuiString转换为std::string 阅读1153次,点赞0次
- C++ - vector存储动态指针时正确释放内存 阅读4164次,点赞0次
- 资源分享 - 3D Math Primer for Graphics and Game Development (First Edition) 英文高清PDF下载 阅读1626次,点赞0次
评论
147