当前位置:网站首页>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
边栏推荐
- 12. Optimization problem practice
- 模型的推理速度
- 12.优化问题实战
- Digital image processing -- Chapter 9 morphological image processing
- Andorid detects GPU rendering speed and over rendering
- Day14. Using interpretable machine learning method to distinguish intestinal tuberculosis and Crohn's disease
- Uboot supports LCD and HDMI to display different logo images
- 19. Up and down sampling and batchnorm
- 关于pytorch反向传播的思考
- 6. Dimension transformation and broadcasting
猜你喜欢
随机推荐
【头歌】重生之我在py入门实训中(10): Numpy
4. Tensor data type and creation tensor
10.梯度、激活函数和loss
方差与协方差
古老的艺术-用好长尾关键词
【头歌】重生之我在py入门实训中(2):公式编程
A photo breaks through the face recognition system: you can nod your head and open your mouth, netizens
数字图像处理 第一章 绪论
[MySQL learning] 8
面试常问Future、FutureTask和CompletableFuture
服务器相关的指标解释
SoK: The Faults in our ASRs: An Overview of Attacks against Automatic Speech Recognition (题目过长)阅读笔记
18.卷积神经网络
【头歌】重生之机器学习-线性回归
Day 4.Social Data Sentiment Analysis: Detection of Adolescent Depression Signals
Activity之应用进程创建流程简析
dpdk 网络协议栈 vpp OvS DDos SDN NFV 虚拟化 高性能专家之路
GBASE 8C——SQL参考6 sql语法(1)
2. Simple regression problem
Gbase 8C - SQL reference 6 SQL syntax (2)









