Skip to content

Instantly share code, notes, and snippets.

@Amanieu
Last active July 4, 2016 23:44
Show Gist options
  • Save Amanieu/0d6227334aa0c67332e46f5a68a9440c to your computer and use it in GitHub Desktop.
Save Amanieu/0d6227334aa0c67332e46f5a68a9440c to your computer and use it in GitHub Desktop.
#include <utility>
#include <iostream>
#include <dlfcn.h>
int foo(char*, int*);
void* dl_handle;
void* get_sym(const char* name)
{
void* s = dlsym(dl_handle, name);
if (!s) {
std::cerr << "dlsym failur";
std::abort();
}
return s;
}
template<const char** name, typename T, T* F>
struct Resolver {};
template<const char** name, typename Ret, typename... Args, Ret (**F)(Args...)>
struct Resolver<name, Ret(*)(Args...), F> {
static Ret run(Args... args)
{
*F = reinterpret_cast<Ret (*)(Args...)>(get_sym(*name));
return (*F)(std::forward<Args>(args)...);
}
};
static const char* _foo_name = "foo";
static decltype(&foo) _foo = Resolver<&_foo_name, decltype(&foo), &_foo>::run;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment