Last active
July 4, 2016 23:44
-
-
Save Amanieu/0d6227334aa0c67332e46f5a68a9440c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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