当前位置:网站首页>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
边栏推荐
- 代码随想录笔记_哈希_242有效的字母异位词
- Day 2. Depressive symptoms, post-traumatic stress symptoms and suicide risk among graduate students
- 2. Simple regression problem
- Digital image processing Chapter 8 - image compression
- 维度问题以及等高线
- 【头歌】重生之我在py入门实训中(10): Numpy
- 神经网络参数初始化
- Speech and Language Processing (3rd ed. draft) Chapter 2 ——正则表达式,文本归一化,编辑距离 阅读笔记
- 数字图像处理——第六章 彩色图像处理
- 西瓜书学习笔记---第一、二章
猜你喜欢

8.数学运算与属性统计

4. Tensor data type and creation tensor

pytorch的多GPU训练的两种方式

Numpy基础学习

pytorch使用data_prefetcher提升数据读取速度

Day 6. Analysis of the energy transmission process of network public opinion in major medical injury events * -- Taking the "Wei Zexi incident" as an example

13. Logistic regression

数字图像处理——第三章 灰度变换与空间滤波

贪心高性能神经网络与AI芯片应用研修

Auto Encoder(AE),Denoising Auto Encoder(DAE), Variational Auto Encoder(VAE) 区别
随机推荐
Andorid detects GPU rendering speed and over rendering
图像超分辨率评价指标
Day 3. Suicidal ideation and behavior in institutions of higher learning: A latent class analysis
Inno setup package jar + H5 + MySQL + redis into exe
【头歌】重生之我在py入门实训中(1)
关于pytorch转onnx经常出现的问题
Gbase 8C - SQL reference 6 SQL syntax (3)
14. Example - Multi classification problem
代码随想录笔记_哈希_242有效的字母异位词
19.上下采样与BatchNorm
[high concurrency] interviewer
6.维度变换和Broadcasting
【头歌】重生之我在py入门实训中(10): Numpy
[MySQL learning] 8
Performance optimization of common ADB commands
Emoji Emoji for text emotion analysis -improving sentimental analysis accuracy with Emoji embedding
Day14. Using interpretable machine learning method to distinguish intestinal tuberculosis and Crohn's disease
Day 17.The role of news sentiment in oil futures returns and volatility forecasting
2. Simple regression problem
Gbase 8C - SQL reference 6 SQL syntax (9)