小编kli*_*kin的帖子

将类成员函数绑定到c函数

下面我有一个将类成员函数绑定到全局函数的概念.这样做的主要目的是使用C++进行C风格的回调函数实现.这可以用更好的方式完成(例如,没有最终的宏typeof,或使用C++ 0x功能)?

#include <iostream>

using namespace std;

template<typename MF> struct mf_wrapper_helper;

template<typename R, typename T>
struct mf_wrapper_helper<R (T::*)()>
{
    template <R (T::*F)()>
    static R wrapper(T *foo) { return (foo->*F)(); }
};

template<typename R, typename T, typename T1>
struct mf_wrapper_helper<R (T::*)(T1)>
{
    template <R (T::*F)(T1)>
    static R wrapper(T *foo, T1 arg1) { return (foo->*F)(arg1); }
};

#define BIND_MF(mf) \
    mf_wrapper_helper<typeof(mf)>::wrapper<mf>


struct Foo
{
    void x() { cout << "Foo::x()" << endl; }
    void x1(int i) { cout << "Foo::x1(" …
Run Code Online (Sandbox Code Playgroud)

c++ templates

5
推荐指数
1
解决办法
1250
查看次数

标签 统计

c++ ×1

templates ×1