2016-08-15: C++ traits
#include <stdio.h> template <typename T>
struct TraitsHelper {
static const bool isPointer = false;
}; template <typename T>
struct TraitsHelper<T*> {
static const bool isPointer = true;
}; template<typename T>
class Test
{
public:
void Func()
{
if(TraitsHelper<T>::isPointer)
{
printf("TypeParm is pointer\n");
}
else
{
printf("TypeParm not pointer\n");
}
}
}; template <typename T>
struct computer_traits {
typedef T param_type;
}; template <>
struct computer_traits<int> {
typedef int param_type;
};
template <>
struct computer_traits<float> {
typedef float param_type;
}; template<typename T>
class Computer
{
public:
typedef T num_type;
typedef typename computer_traits<num_type>::param_type traits_type;
traits_type Add(traits_type a, traits_type b)
{
return a+b;
}
}; int main()
{
Test<int> intTest;
intTest.Func(); Test<int*> pointerTest;
pointerTest.Func(); Computer<float> computerFloat;
printf("float add = %f\n", computerFloat.Add(3.2, 5.0)); Computer<int> computerInt;
printf("float add = %d\n", computerInt.Add(3.2, 5.0)); return 0;
}
参考资料
http://www.cnblogs.com/youthlion/archive/2011/12/01/2255618.html
http://blog.csdn.net/my_business/article/details/7891687
http://www.cnblogs.com/pugang/archive/2012/10/17/2727378.html
2016-08-15: C++ traits的更多相关文章
- http://tedhacker.top/2016/08/05/Spring%E7%BA%BF%E7%A8%8B%E6%B1%A0%E4%BD%BF%E7%94%A8%E6%96%B9%E6%B3%95/
http://tedhacker.top/2016/08/05/Spring%E7%BA%BF%E7%A8%8B%E6%B1%A0%E4%BD%BF%E7%94%A8%E6%96%B9%E6%B3%9 ...
- MyBatis 配置/注解 SQL CRUD 经典解决方案(2019.08.15持续更新)
本文旨在记录使用各位大神的经典解决方案. 2019.08.14 更新 Mybatis saveOrUpdate SelectKey非主键的使用 MyBatis实现SaveOrUpdate mybati ...
- 2016.8.15上午纪中初中部NOIP普及组比赛
2016.8.15上午纪中初中部NOIP普及组比赛 链接:https://jzoj.net/junior/#contest/home/1333 这次比赛不怎么好,因为这套题目我并不是很擅长. 可同学们 ...
- 2016.9.15初中部上午NOIP普及组比赛总结
2016.9.15初中部上午NOIP普及组比赛总结 2016.09.15[初中部 NOIP普及组 ]模拟赛 又翻车了!表示时超和空超很可恨! 进度 比赛:AC+0+0+20=120 改题:AC+80+ ...
- 最新版Theos.2016.08的安装方法
http://bbs.pediy.com/showthread.php?t=212425 标题: [翻译]手把手安装最新版Theos.2016.08作者: roysue时间: 2016-08-26,1 ...
- macbook pro 2016 2017 15寸 雷电3 外接显卡 epu 简单教程(不修改UEFI)
雷电3外接显卡效果还不错,但是除了akitio node 其他厂家并不会维护自己的固件来适配新机型,我自己买的mbp 2016 15''就出现了和AORUS Gaming Box 1070不兼容的问题 ...
- [2016.08.09]文本替换专家 v5.2
软件名称:文本替换专家最新版本:v5.2软件授权:共享软件发布日期:2016.01.18文件大小:650KB操作系统:XP/2003/Win7/Win2008开发人员:胡洪祥版权所有:胡洪祥
- 2016 08 27 印刷菜单增加sql语句
insert into `module` (`ID`, `CONSONANTCODE`, `CREATEDATE`, `DESCRIPTION`, `HANDLER`, `HASCHILD`, `IC ...
- CentOS 设置网络(修改IP&修改网关&修改DNS)--update.14.08.15
自己电脑上装的虚拟机用桥接方式连接物理机,虚拟机重启后ip会发生变化,非常阻碍Xshell的连接和hosts指定的dns. 通过修改IP为static模式,保持IP不变. ============== ...
- [位运算] [搜索] [递推优化] [计算几何] TEST 2016.7.15
NOIP2014 提高组模拟试题 第一试试题 题目概况: 中文题目名称 合理种植 排队 科技节 源程序文件名 plant.pas/.c/.cpp lineup.pas/.c/.cpp scifest. ...
随机推荐
- <我是一只IT小小鸟>读书笔记
这篇文章给我感触最深的是开篇蒋宇东所出的一道选择题--今后的发展选择有三条:A.做一辈子IT民工:B.将大学时欠下来的债补上:C.改行. 他们用自己的成长故事告诉师弟师妹们:一定要弄清楚上大学首要的任 ...
- Ubuntu Command-Line: Enable Unlimited Scrolling in the Terminal
At times when using the terminal, the output from a command can be so long, you simply can’t scroll ...
- CentOS上的RabbitMQ安装
1. erlang安装配置(这里我们在opt目录下进行安装配置) cd /opt 安装依赖文件: yum install gcc glibc-devel make ncurses-devel open ...
- Android TextView图文混合编排
Android TextView图文混合编排 实现技术细节不难,两个要点:1.html代码的混合编写.2,重写ImageGetter.例如:布局: <?xml version="1.0 ...
- MATLAB与C/C++混合编程的一些总结
[转载请注明出处]http://www.cnblogs.com/mashiqi 先上总结: 由于C/C++语言的函数输入输出参数的特点,可以将多个参数方便地传入一个函数中,但却不能方便地返回多个参数. ...
- notifyDataSetInvalidated()跟notifyDataSetChanged()的区别
public void notifyDataSetChanged(): 通过一个外部的方法控制,如果适配器的内容改变了,那么就会强制调用getView来刷新每个Item的内容.这个方法内部实现了在每个 ...
- [转载]MCU DSP ARM 嵌入式 之间的区别
2016-06-28 10:31:24) 嵌入式"这是个概念,准确的定义没有,各个书上都有各自的定义.但是主要思想是一样的,就是相比较PC机这种通用系统来说,嵌入式系统是个专用系统,结构精简 ...
- codeforces 199a
link:http://codeforces.com/contest/342/problem/A 恩恩,读错题了.人家是at most 7,我理解成了at lease 7.好欢乐~ #include ...
- hdu4690 EBCDIC ——水题,考耐心
link:http://acm.hdu.edu.cn/showproblem.php?pid=4690 考的是耐心何细心啊,用map把两个表格映射一下就行~ #include <iostream ...
- 当pip install不能正确安装的时候,try easy_install
当pip install不能正确安装的时候,try easy_install 重复试了几次pip install -r requirements.txt,都在安装pillow的时候失败了,想找这个枕头 ...