Is it possible to make a generic C++ function pointer wrapper with NULL checks?
<p>I have to wrap several function pointers which can have NULL values. I'm trying to make a general class template for this, which checks if the function pointer is NULL, calls the original function if pointer is not NULL, and returns a default value if the pointer is NULL. Something like this:</p>

<pre><code>//Function typedefs
typedef void(*foo)();
typedef int(*bar)(int u, char *);

//Function pointers
foo gfFoo;
bar gfBar;

//Wrapper class
template&lt;typename T,  typename... Args&gt;
class WrapFuncObj {
    T* f;
public:
    WrapFuncObj(T* t) {
        f = t;
    }
    typename std::result_of&lt;T(Args...)&gt;::type operator()(Args&amp;&amp;... args) {
        if(f != nullptr){
            return (*f)(std::forward&lt;Args&gt;(args)...);
        }
        else
        {
            return 0; //RETURN something for non-void types
        }
    }
};

template&lt;typename T,  typename... Args&gt;
class WrapFuncObjVoid {
    T* f;
public:
    WrapFuncObjVoid(T* t) {
        f = t;
    }
    typename std::result_of&lt;T(Args...)&gt;::type operator()(Args&amp;&amp;... args) {
        if(f != nullptr){
            (*f)(std::forward&lt;Args&gt;(args)...);
        }
        else
        {
            //DO Nothing for void 
        }
    }
};

//Usage:
WrapFuncObjVoid&lt;foo&gt; WrapFoo(&amp;gfFoo);
WrapFoo();
WrapFuncObj&lt;bar, int, char*&gt; WrapBar(&amp;gfBar); 
WrapBar(0, "test");
</code></pre>

<p>As can be seen, there needs to be separate templates for non-void and void types because in case of null, nothing should be returned if the function pointer is NULL. How to circumvent this problem and make a single template enough for both?</p>