Typesafety with dlsym function loading
[ I'm assuming your example is meant to return x(dpy, drawable); rather than call itself recursively. ]


It's worth it if you have a use for it I guess.
Printing to std::cerr is not error handling, it's error logging. Pick a real error handling strategy and stick to it. Similarly is printing to std::cout necessary? I also believe that the fewer library calls there are in the constructor, the fewer risks of your wrapper calling itself recursively during static initialization of x, which will be disastrous.


Posix recommends the following way to cast when dealing with function types with dlsym:

*(void**)&amp;f = dlsym(RTLD_NEXT, sym.c_str());


operator() can use perfect forwarding:

Ret
operator()(Args... args)
{
    f(std::forward&lt;Args&gt;(args));
}


The wrapping will cost two moves or one copy + one move per argument passed by value and one forward for reference types. It is transparent to the client; he or she won't need to use std::move unless the original function would already have him required to. Notice that the first copy or move of a by-value argument must be paid no matter what; the real opportunity cost of the wrapper is the (potential) move that comes after that, to pass the argument to the real function.

By comparison using Args const&amp;... breaks if the wrapped function is void foo(T&amp;&amp;);, due to reference collapsing rules the signature will be void operator()(T&amp;);

Perhaps the macro should take the name of the variable as an argument; but that may not be necessary depending on your planned usage.

Minor pet peeve: meaningful names. I would have called the constructor parameter symbol. The time it takes to type three more characters each time is simply negligible compared to the time it takes to write and maintain code. function_type instead of real_func possibly, too.