[百度空间] [note] pointer to member is a POD type
C++03 3.9-10:
|
1
|
|
As we all knows the pointer to member may not be cast into void* (GCC is OK but it's a special case anyway), according to the standrad we can store the pointer to member to a char buffer though. Unfortunately, the size of "pointer to member" is implementation defined, so we still need some hack for different compilers.
here's a table for sizeof(pointer to member) for some compilers
(http://www.codeproject.com/Articles/7150/Member-Function-Pointers-and-the-Fastest-Possible)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
Compiler Options Single Multi Virtual Unknown MSVC 4 8 12 16 MSVC /vmg 16# 16# 16# 16 MSVC /vmg /vmm 8# 8# -- 8 Intel_IA32 4 8 12 16 Intel_IA32 /vmg /vmm 4 8 -- 8 Intel_Itanium 8 12 16 20 G++ 8 8 8 8 Comeau 8 8 8 8 DMC 4 4 4 4 BCC32 12 12 12 12 BCC32 /Vmd 4 8 12 12 WCL386 12 12 12 12 CodeWarrior 12 12 12 12 XLC 20 20 20 20 DMC small 2 2 2 2 medium 4 4 4 4 WCL small 6 6 6 6 compact 6 6 6 6 medium 8 8 8 8 large 8 8 8 8 |
the original article implement each compiler's pointer to member mechanism, which I believe is insane, it's definitely not a good way to write portable codes.here's what I'm doing (code snippet).
所以pointer to member 可以被保存在byte array 里面再被解出来.这对于做delegate很有帮助.这是我目前使用的方法.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
class Delegate{public://per-compiler constant, or use a max enough size for all compilers#if COMPILER == COMPILER_MSVC static const size_t MAX_POINTER2MEMBER_SIZE = 16;#elif COMPILER == COMPILER_GNUC static const size_t MAX_POINTER2MEMBER_SIZE = 8;#else static const size_t MAX_POINTER2MEMBER_SIZE = 20;#endif void* mPtr; char mMemberRaw[MAX_POINTER2MEMBER_SIZE ]; //store the pointer to member to a byte buffer template <typename T> explicit Delegate(T* ptr, void (T::*func)(void*) ) { assert( sizeof(func) <= MAX_SIZE ); ::memset(mMemberRaw, 0, MAX_SIZE); ::memcpy(mMemberRaw, &func, sizeof(func) ); }};//invoke function: extract the "pointer to member" from the byte buffer{ ... typedef void (T::*TMemberFn)(void*); TMemberFn TFn = NULL; ::memcpy(&TFn, mMemberRaw, sizeof(TFn) ); ( ((T*)mPtr)->*TFn )( data);} |
there's a very efficient way to do the delegate :
http://www.codeproject.com/Articles/11015/The-Impossibly-Fast-C-Delegates
他也是用模板函数,但不同的是把函数地址作为模板参数.本来打算改用这个方法的.但是唯一的缺点就是需要显式指定模板参数<>来实例化, 而构造函数只能通过TAD来套用模板参数,不能显式指定...所以他用的是一个静态模板函数来构造一个对象
|
1
|
delegate d = delegate::frommember<T,&T::func>( pOjb); //这样稍微有点恶心. |
而我使用的方式虽然多了一个char[], 有一点点额外的内存开销和运行时开销,但是大多数情况下几乎可以忽略不计.
主要是可以通过Template Arugment Deduction 直接创建delegate, 比如:
|
1
|
Delegate d(pObj, &CLASS::function); |
所以考虑了一下,决定暂时不用他的那种方法(用起来稍微不方便,但是效率非常高).以后看情况再考虑要不要改.
另外,作为一个C++程序猿, 个人觉得"数据绑定", "反射","代理",这些使用时尽量谨慎, 不可滥用. 除了在event/msg 和 UI/Tools的极少数地方要用以外,其他地方还是尽量不要用为好. 最近把数据绑定框架做了简单重构, 幸好用到的地方不多(主要是编辑器在用), 要不然重构就哭了. 目前还没有反射,暂时不需要, 以后需要时再加.最好是另外封装,不影响现在的代码.
[百度空间] [note] pointer to member is a POD type的更多相关文章
- jquery-plugin-biggerLink,highLight-层加亮_andy 阳光生活_百度空间
How to get the xpath by clicking an html element How to get the xpath by clicking an html element Qu ...
- Android手机无法访问百度空间的解决办法
本文网址:http://www.cnblogs.com/tunnel213/p/4301165.html 现象: 百度“JavaScript函数高级”后找到一篇文章,百度空间的,无法查看: 配置: 三 ...
- document.domain - JavaScript的同源策略问题:错误信息:Permission denied to access property 'document'_eecc00_百度空间
document.domain - JavaScript的同源策略问题:错误信息:Permission denied to access property 'document'_eecc00_百度空间 ...
- memcached vs MySQL Memory engine table 速度比较_XMPP Jabber即时通讯开发实践_百度空间
memcached vs MySQL Memory engine table 速度比较_XMPP Jabber即时通讯开发实践_百度空间 memcached vs MySQL Memory engin ...
- 提高mysql memory(heap) engine内存性能的开源补丁_XMPP Jabber即时通讯开发实践_百度空间
提高mysql memory(heap) engine内存性能的开源补丁_XMPP Jabber即时通讯开发实践_百度空间 提高mysql memory(heap) engine内存性能的开源补丁
- sort 使用 tab键 作为 分隔符_人生如梦_百度空间
sort 使用 tab键 作为 分隔符_人生如梦_百度空间 sort 使用 tab键 作为 分隔符 For some reason "\t" doesn't work right, ...
- centos5.5字体为方块问题的解决_深入学习编程_百度空间
centos5.5字体为方块问题的解决_深入学习编程_百度空间 centos5.5字体为方块问题的解决 一.yum -y install fonts-chinese二.yum -y install f ...
- 毕业论文评审意见、导师意见范文、模板_Smile~风_百度空间
body{ font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI& ...
- Attribute name invalid for tag form according to TLD异常解决办法_gaigai_百度空间
body{ font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI& ...
随机推荐
- Delphi初学者应小心的六大陷阱
Delphi初学者应小心的六大陷阱 作者:子夜编译 初学DelphiI的人,由于各种原因,对DelphiI中的许多概念不能很好的理解,并由此带来了许多的问题,或者是开发出的程序稳性不好 ...
- L2-015. 互评成绩
学生互评作业的简单规则是这样定的:每个人的作业会被k个同学评审,得到k个成绩.系统需要去掉一个最高分和一个最低分,将剩下的分数取平均,就得到这个学生的最后成绩.本题就要求你编写这个互评系统的算分模块. ...
- Linux分区
硬盘分区主要分为基本分区和扩展分区两种,基本分区和扩展分区的数目之和不能大于四个.且基本分区可以马上被使用但不能再分区.扩展分区必须再进行分区后才能进行使用,也就是说它必须进行二次分区.扩展分区再分下 ...
- Linux查看文件以及文件夹的大小
df可以查看一级文件夹大小.使用比例.档案系统及其挂入点,但对文件却无能为力.du可以查看文件及文件夹的大小. df命令可以显示目前所有文件系统的可用空间及使用情形,请看下列这个例子: df命令可以查 ...
- chkconfig 命令详解
chkconfig命令主要用来更新(启动或停止)和查询系统服务的运行级信息.谨记chkconfig不是立即自动禁止或激活一个服务,它只是简单的改变了符号连接. 使用语法: chkconfig [--a ...
- VS中制作安装文件
第一步先来建一个最简单的Windows窗体应用程序,并为项目命名为WinFormTest,解决方案为WinFormSetup 第二步在窗体Form中添加一个按钮并在按钮事件中添加代码,只做测试因此简单 ...
- oracle expdp impdp
一.不管导入还有导出都要先创建目录 1.创建目录 create directory my_dir as 'd:\yth';--生成目录(必须在指定位置先创建文件夹,名称最好与用户名一致) yth:是目 ...
- 通过Roslyn构建自己的C#脚本
通过Roslyn构建自己的C#脚本 在下一代的C#中,一个重要的特性就是"Compiler as a Service",简单的讲,就是就是将编译器开放为一种可在代码中调用的服务.最 ...
- Go字典
字典(map)是Go语言内置的数据结构,一组键值对的无序集合. 看代码: package main import "fmt" func main() { //使用make申请一个m ...
- Django+Nginx+MongoDB+Mysql+uWsgi的搭建
搭建目标如下: 图:系统架构图 这个系统可以提供web服务及其它查询应用服务,我用其做一个二手房信息搜集.处理及分发的系统,可以通过浏览器访问,也可以通过定制的客户端进行访问. 一.安装篇 1.下载安 ...