当前位置:网站首页>std::bind与std::function的一些应用
std::bind与std::function的一些应用
2022-07-27 05:19:00 【Mr FF】
#include <functional>
#include <iostream>
#include <typeinfo>
#include <typeindex>
#include <unordered_map>
class Type {
};
class A {
public:
static A* instance = nullptr;
Type GetUint32Type()
{
Type type;
// your businsee
return type;
}
Type GetInt32Type()
{
Type type;
// your businsee
return type;
}
Type* GetInstance()
{
if (instance == nullptr) {
instance = new A();
}
return instance;
}
};
using CreateType = std::function<Type()>;
std::unordered_map<std::type_index, CreateType> g_typeTable =
{
{typeid(uint32_t), std::bind(&A::GetUint32Type, A::GetInstance())},
{typeid(int32_t), std::bind(&A::GetInt32Type, A::GetInstance())}
};
应用场景:使用typeid方法获取类型,根据不同的类型去创建对应的封装。
避免出现大量的if/else
边栏推荐
- 李宏毅 2020 深度学习与人类语言处理 DLHLP-Conditional Generation by RNN and Attention-p22
- Andorid detects GPU rendering speed and over rendering
- 16. Over fitting and under fitting
- Digital image processing Chapter 5 - image restoration and reconstruction
- Activity之应用进程创建流程简析
- Day 15. Deep learning radiomics can predict axillary lymphnode status in early-stage breast cancer
- Day10. Work organization and mental health problems in PhD students
- 7.合并与分割
- 1.PyTorch简介
- 面试常问Future、FutureTask和CompletableFuture
猜你喜欢

为什么交叉熵损失可以用于刻画损失

李宏毅 2020 深度学习与人类语言处理 DLHLP-Conditional Generation by RNN and Attention-p22

17.动量与学习率的衰减

Digital image processing -- Chapter 9 morphological image processing

cycleGAN解析
![[MVC Architecture] MVC model](/img/71/e10da490d5f0098c64b33e77d158e7.png)
[MVC Architecture] MVC model

基于深度神经网络的社交媒体用户级心理压力检测

Emoji Emoji for text emotion analysis -improving sentimental analysis accuracy with Emoji embedding

Digital image processing Chapter 8 - image compression

leetcode系列(一):买卖股票
随机推荐
数字图像处理——第三章 灰度变换与空间滤波
Digital image processing Chapter 4 - frequency domain filtering
Rk3399 GPIO port how to find which GPIO port it is
8.数学运算与属性统计
方差与协方差
小技巧-彻底删除U盘中的文件
A photo breaks through the face recognition system: you can nod your head and open your mouth, netizens
一张照片攻破人脸识别系统:能点头摇头张嘴,网友
图像超分辨率评价指标
Chrome 如何快速将一组正在浏览的网页(tabs)转移到另一台设备(电脑)上
Day 3. Suicidal ideation and behavior in institutions of higher learning: A latent class analysis
Day14. 用可解释机器学习方法鉴别肠结核和克罗恩病
李宏毅 2020 深度学习与人类语言处理 DLHLP-Conditional Generation by RNN and Attention-p22
【并发编程系列9】阻塞队列之PriorityBlockingQueue,DelayQueue原理分析
andorid检测GPU呈现速度和过度绘制
2. Simple regression problem
新冠时空分析——Global evidence of expressed sentiment alterations during the COVID-19 pandemic
面试常问Future、FutureTask和CompletableFuture
Day10. Work organization and mental health problems in PhD students
14. Example - Multi classification problem