当前位置:网站首页>Using registered classes to realize specific type matching function template
Using registered classes to realize specific type matching function template
2022-07-03 13:49:00 【yolo_ yyh】
Function templates allow different types to call code with the same function , It can reduce the redundancy of code , But what if you only want specific types to use function templates ? It can be implemented by template specialization and registration classes .
The code is as follows :
#include <iostream>
template <typename T>
struct Register{};
template <>
struct Register<int> {
typedef int Type;
constexpr static const char* TypeName = "int";
};
template <>
struct Register<float> {
typedef float Type;
constexpr static const char* TypeName = "float";
};
template <>
struct Register<long> {
typedef long Type;
constexpr static const char* TypeName = "long";
};
template <typename T, typename = typename Register<T>::Type>
void print(const T& x) {
std::cout << Register<T>::TypeName << '\t' << x << std::endl;
}
int main() {
print(-1);
print(2147483650);
print(12.11); // double type , Mismatch
return 0;
}
All to Register Registered type , Can be matched successfully print function , Undirected Register The registered type will report an error when compiling , The error information is as follows :

边栏推荐
- Go 1.16.4: purpose of go mod tidy
- Another industry has been broken by Chinese chips. No wonder the leading analog chip companies in the United States have cut prices and sold off
- Bidirectional linked list (we only need to pay attention to insert and delete functions)
- 记录关于银行回调post请求405 问题
- AI 考高数得分 81,网友:AI 模型也免不了“内卷”!
- [understanding by chance-37]: the structure of human sensory system determines that human beings are self-centered
- Kivy教程之 盒子布局 BoxLayout将子项排列在垂直或水平框中(教程含源码)
- JVM系列——概述,程序计数器day1-1
- Disruptor -- a high concurrency and high performance queue framework for processing tens of millions of levels
- [bw16 application] instructions for firmware burning of Anxin Ke bw16 module and development board update
猜你喜欢

MySQL functions and related cases and exercises

PhpMyAdmin stage file contains analysis traceability

Mastering the cypress command line options is the basis for truly mastering cypress

Use docker to build sqli lab environment and upload labs environment, and the operation steps are provided with screenshots.

Brief analysis of tensorboard visual processing cases

logback日志的整理

The latest BSC can pay dividends. Any B usdt Shib eth dividend destruction marketing can

Universal dividend source code, supports the dividend of any B on the BSC

Comprehensive evaluation of double chain notes remnote: fast input, PDF reading, interval repetition / memory

双向链表(我们只需要关注插入和删除函数)
随机推荐
This math book, which has been written by senior ml researchers for 7 years, is available in free electronic version
The shadow of the object at the edge of the untiy world flickers, and the shadow of the object near the far point is normal
Mysql:insert date:SQL 错误 [1292] [22001]: Data truncation: Incorrect date value:
Leetcode-1175. Prime Arrangements
Bidirectional linked list (we only need to pay attention to insert and delete functions)
MapReduce实现矩阵乘法–实现代码
Swiftui development experience: the five most powerful principles that a programmer needs to master
RichView TRVStyle ListStyle 列表样式(项目符号编号)
Unity embeddedbrowser browser plug-in event communication
Several common optimization methods matlab principle and depth analysis
Go language web development series 29: Gin framework uses gin contrib / sessions library to manage sessions (based on cookies)
Kivy教程之 如何通过字符串方式载入kv文件设计界面(教程含源码)
Unable to stop it, domestic chips have made another breakthrough, and some links have reached 4nm
Logback log sorting
又一个行业被中国芯片打破空白,难怪美国模拟芯片龙头降价抛售了
Heap structure and heap sort heapify
Go language unit test 4: go language uses gomonkey to test functions or methods
Error handling when adding files to SVN:.... \conf\svnserve conf:12: Option expected
Kivy tutorial how to automatically load kV files
[bw16 application] instructions for firmware burning of Anxin Ke bw16 module and development board update