您的位置:首页 > 其它

[百度空间] [note] pointer to member is a POD type

2015-05-18 18:43 295 查看
C++033.9-10:

Arithmetictypes(
3.9
.
1
),enumerationtypes,pointertypes,andpointertomembertypes(
3.9
.
2
),andcv-qualifiedversionsofthesetypes(
3.9
.
3
)arecollectivelycalledscalartypes.Scalartypes,POD-structtypes,POD-uniontypes(clause
9
),arraysofsuchtypesandcv-qualifiedversionsofthesetypes(
3.9
.
3
)arecollectivelycalledPODtypes.




Asweallknowsthepointertomembermaynotbecastintovoid*(GCCisOKbutit'saspecialcaseanyway),accordingtothestandradwecanstorethepointertomembertoacharbufferthough.Unfortunately,thesizeof"pointertomember"isimplementationdefined,sowestillneedsomehackfordifferentcompilers.

here'satableforsizeof(pointertomember)forsomecompilers

(http://www.codeproject.com/Articles/7150/Member-Function-Pointers-and-the-Fastest-Possible)

theoriginalarticleimplementeachcompiler'spointertomembermechanism,whichIbelieveisinsane,it'sdefinitelynotagoodwaytowriteportablecodes.here'swhatI'mdoing(codesnippet).

所以pointertomember可以被保存在bytearray里面再被解出来.这对于做delegate很有帮助.这是我目前使用的方法.

there'saveryefficientwaytodothedelegate:

http://www.codeproject.com/Articles/11015/The-Impossibly-Fast-C-Delegates

他也是用模板函数,但不同的是把函数地址作为模板参数.本来打算改用这个方法的.但是唯一的缺点就是需要显式指定模板参数<>来实例化,而构造函数只能通过TAD来套用模板参数,不能显式指定...所以他用的是一个静态模板函数来构造一个对象

而我使用的方式虽然多了一个char[],有一点点额外的内存开销和运行时开销,但是大多数情况下几乎可以忽略不计.

主要是可以通过TemplateArugmentDeduction直接创建delegate,比如:

所以考虑了一下,决定暂时不用他的那种方法(用起来稍微不方便,但是效率非常高).以后看情况再考虑要不要改.

另外,作为一个C++程序猿,个人觉得"数据绑定","反射","代理",这些使用时尽量谨慎,不可滥用.除了在event/msg和UI/Tools的极少数地方要用以外,其他地方还是尽量不要用为好.最近把数据绑定框架做了简单重构,幸好用到的地方不多(主要是编辑器在用),要不然重构就哭了.目前还没有反射,暂时不需要,以后需要时再加.最好是另外封装,不影响现在的代码.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐