C++03 3.9-10:

1

Arithmetic types (3.9.1), enumeration types, pointer types, and pointer to member types (3.9.2), and cv-qualified versions of these types (3.9.3) are collectively called scalar types. Scalar types, POD-struct types, POD-union types (clause 9), arrays of such types and cv-qualified versions of these types (3.9.3) are collectively called POD types.

 

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*) )
    {
        assertsizeof(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的更多相关文章

  1. 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 ...

  2. Android手机无法访问百度空间的解决办法

    本文网址:http://www.cnblogs.com/tunnel213/p/4301165.html 现象: 百度“JavaScript函数高级”后找到一篇文章,百度空间的,无法查看: 配置: 三 ...

  3. document.domain - JavaScript的同源策略问题:错误信息:Permission denied to access property 'document'_eecc00_百度空间

    document.domain - JavaScript的同源策略问题:错误信息:Permission denied to access property 'document'_eecc00_百度空间 ...

  4. memcached vs MySQL Memory engine table 速度比较_XMPP Jabber即时通讯开发实践_百度空间

    memcached vs MySQL Memory engine table 速度比较_XMPP Jabber即时通讯开发实践_百度空间 memcached vs MySQL Memory engin ...

  5. 提高mysql memory(heap) engine内存性能的开源补丁_XMPP Jabber即时通讯开发实践_百度空间

    提高mysql memory(heap) engine内存性能的开源补丁_XMPP Jabber即时通讯开发实践_百度空间 提高mysql memory(heap) engine内存性能的开源补丁

  6. sort 使用 tab键 作为 分隔符_人生如梦_百度空间

    sort 使用 tab键 作为 分隔符_人生如梦_百度空间 sort 使用 tab键 作为 分隔符 For some reason "\t" doesn't work right, ...

  7. centos5.5字体为方块问题的解决_深入学习编程_百度空间

    centos5.5字体为方块问题的解决_深入学习编程_百度空间 centos5.5字体为方块问题的解决 一.yum -y install fonts-chinese二.yum -y install f ...

  8. 毕业论文评审意见、导师意见范文、模板_Smile~风_百度空间

    body{ font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI& ...

  9. Attribute name invalid for tag form according to TLD异常解决办法_gaigai_百度空间

    body{ font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI& ...

随机推荐

  1. Ubuntu10.10的网络配置

    有一阵子着实对Ubuntu的网络配置很迷惑,耐下心来仔细上网找了找,有点小心得,总结一下. 先说下大概的配置过程,再去细究一些情况. 一.配置大概分三类:通过配置文件配置.通过命令配置.通过图形化的网 ...

  2. 通过URLHttpConnection方式连接网络步骤,获取位图为例

    要注意的是:访问网络不能直接放在主线程,要放在另外一个线程里面,如果放在主线程会报android.os.NetworkOnMainThreadException错误1 public Bitmap ge ...

  3. 9.css背景

    这节也是关于盒模型的扩展,而我个人喜欢把盒模型想象成图画.元素的尺寸调整就是画布大小的调整,边框的设置就是画框的镶嵌.但是,作为一个最终要将画作展现到美术馆(网页)的艺术家来说,仅仅是这样还是不够的, ...

  4. 第一步 django的下载安装

    django是python众多web框架中比较有名的一个,以大包大揽功能俱全而著名.但作为重量级的web框架,难免性能上回有所损失,不过由于其封装了各种API,在开发的时候会便利许多.所以也是深受欢迎 ...

  5. sizeclass

    横竖9种组合,代表所有大小屏幕,在storyboard中可以把contrans与不同组合绑定,也就是说,可能横向有多一个约束,纵向就没了... 实现不同屏幕不同约束,这应该是sizeclass 的存在 ...

  6. WPF中线性渐变画刷的一个小窍门

    最近被项目里面控件的设计搞的死去活来的,大部分的设计都会需要使用进度条的功能,因为UI形状的变态,使用ProgressBar不能满足需求,没办法就自己想办法实现进度显示.折腾的多了发现一个很不错的方法 ...

  7. linux rm 命令

    1.命令格式: rm [选项] 文件… 2.命令功能: 删除一个目录中的一个或多个文件或目录,如果没有使用- r选项,则rm不会删除目录.如果使用 rm 来删除文件,通常仍可以将该文件恢复原状. 3. ...

  8. 019C#中使用移位运算符获取汉字编码值

    在进行移位运算时,当数值的二进制数每次向左移1位就相当于乘以2,当数值每次向右移一位就相当于除以2 private void button1_Click(object sender, EventArg ...

  9. hdu 2579 Dating with girls(2)

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2579 Dating with girls(2) Description If you have sol ...

  10. SQL中一种类似GUID值的函数实现

        开发中会需要用到多列值组合成一个ID值的情况.比如做数据清洗的时候,一张表A有五列,分别是医院.科室.医生.职称.电话.面有许多重复的数据需要和另一个表B(和A列相同)做对比.清洗需要做两件事 ...