Skip to content

Instantly share code, notes, and snippets.

@dgodfrey206
Last active February 23, 2026 16:56
Show Gist options
  • Select an option

  • Save dgodfrey206/bbd816f95e990eb89f593c30797ee130 to your computer and use it in GitHub Desktop.

Select an option

Save dgodfrey206/bbd816f95e990eb89f593c30797ee130 to your computer and use it in GitHub Desktop.
#include <any>
#include <print>
#include <string>
#include<bits/stdc++.h>
using namespace std;
struct poly_base {
virtual void operator()(std::vector<std::any> args) = 0;
virtual ~poly_base()=default;
};
template <class Sig>
struct poly_derived;
template<class R, class... Args>
struct poly_derived<R(Args...)> : public poly_base {
poly_derived(std::function<R(Args...)> d) : data(d) {}
std::function<R(Args...)> data;
void operator()(std::vector<std::any> args) override {
if (args.size() > 0)
do_invoke(data, args);
}
void do_invoke(std::function<R(Args...)> f, std::vector<std::any> args) {
[&]<std::size_t... Is>(std::index_sequence<Is...>) {
data( std::any_cast<Args>(args[Is])... );
}(std::index_sequence_for<Args...>{});
}
};
template<class... Args>
void foo(unique_ptr<poly_base>&& f, Args&&... args) {
std::vector<std::any> arguments;
(arguments.emplace_back(std::forward<Args>(args)), ...);
(*f)(arguments);
}
int main() {
foo(std::make_unique<poly_derived<void(double)>>([](double) { std::println("double"); }));
}
#include <any>
#include <print>
#include <string>
#include<bits/stdc++.h>
using namespace std;
struct poly_base {
virtual void operator()(std::vector<std::any> args) = 0;
virtual ~poly_base()=default;
};
template <class Sig>
struct poly_derived;
template<class R, class... Args>
struct poly_derived<R(Args...)> : public poly_base {
poly_derived(std::function<R(Args...)> d) : data(d) {}
std::function<R(Args...)> data;
void operator()(std::vector<std::any> args) override {
if (args.size() > 0)
do_invoke(data, args);
}
void do_invoke(std::function<R(Args...)> f, std::vector<std::any> args) {
if constexpr (sizeof...(Args) > 0) {
[&]<std::size_t... Is>(std::index_sequence<Is...>) {
data( std::any_cast<Args>(args[Is])... );
}(std::index_sequence_for<Args...>{});
}
}
};
template<class... Args>
void foo(unique_ptr<poly_base>&& f, Args&&... args) {
std::vector<std::any> arguments;
(arguments.emplace_back(std::forward<Args>(args)), ...);
(*f)(arguments);
}
struct S {
template<class T,class D=std::decay_t<T>>
S(T&& t)
: callable( std::make_unique<poly_derived<int(int)>>([t=std::move(t)](int other) mutable {
t = std::move(other) + 7;
return t;
}) )
, invoker([](std::unique_ptr<poly_base>& base, void* p) {
std::vector<std::any> args;
args.emplace_back( std::move(*static_cast<std::decay_t<T>*>(p)) );
(*base)( args );
})
{}
void invoke() {
double x;
invoker(callable, &x);
}
std::unique_ptr<poly_base> callable;
std::vector<std::any> arguments;
void(*invoker)(std::unique_ptr<poly_base>&, void*);
};
int main() {
S s(5);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment