Skip to content

Instantly share code, notes, and snippets.

@Pibben
Created September 2, 2019 18:13
Show Gist options
  • Save Pibben/9ff01b67232944d76f68d0d41b494d2f to your computer and use it in GitHub Desktop.
Save Pibben/9ff01b67232944d76f68d0d41b494d2f to your computer and use it in GitHub Desktop.
#include <functional>
#include <string>
#include <tuple>
#include <array>
struct Thread {
struct InternalBase {
virtual void call() = 0;
};
InternalBase* ibptr;
template <class Ret, class... Args>
struct Internal : public InternalBase {
Ret(*mFunc)(Args...);
std::tuple<Args...> mArgs;
Internal(Ret(*func)(Args...), Args... args) : mFunc(func), mArgs(std::forward<Args>(args)...) { }
void call() override {
std::apply(mFunc, mArgs);
}
};
template <class Ret, class... Args>
Thread(Ret(*func)(Args...), Args... args) {
ibptr = new Internal<Ret, Args...>(func, args...);
}
void call() {
ibptr->call();
}
};
static void func(int a, double d) {
printf("%d\n", a);
}
int main() {
Thread a(func, 1, 1.0);
a.call();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment