小编Mit*_*oft的帖子

使用C的'...'参数包作为C++模板值?

我花了最近几天尝试在C++中为函数指针创建一个通用的包装器,我已经设法解决了几乎每一个问题.我的主要目标是能够简单地将对象作为函数调用内部存储的函数指针.如果指针指向某处,那么它会将其称为正常,而空指针只是不会调用该函数,它会继续,就好像什么都没发生一样.我打算主要用于回调函数,我可能不关心函数是否被调用,只是想要执行一个动作.它与以下几乎完美配合:

template<typename T>
class Action;

template<typename TReturn, typename ... TArgs>
class Action<TReturn(TArgs...)> {
public:
    //! Define a type for the Action object
    typedef TReturn(*signature)(TArgs...);

    //! Constructors
    inline Action(const signature& pFunc = nullptr) : mPointer(pFunc) {}
    inline Action(const Action& pCopy) : mPointer(pCopy.mPointer) {}

    //! Operator Call
    inline bool operator() (TReturn& pReturn, TArgs ... pArgs) const { if (!mPointer) return false; pReturn = mPointer(pArgs...); return true; }

    //! Operators
    inline Action& operator=(const Action& pCopy) { mPointer = pCopy.mPointer; return *this; }
    inline …
Run Code Online (Sandbox Code Playgroud)

c++ parameters templates function-pointers

6
推荐指数
1
解决办法
197
查看次数

标签 统计

c++ ×1

function-pointers ×1

parameters ×1

templates ×1