Skip to content

Instantly share code, notes, and snippets.

@dgodfrey206
Last active February 26, 2026 18:44
Show Gist options
  • Select an option

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

Select an option

Save dgodfrey206/42b4fb80910fdffd38ba521a022c42c5 to your computer and use it in GitHub Desktop.
#include <any>
#include <print>
#include <string>
using namespace std;
#pragma GCC diagnostic ignored "-Wnon-template-friend"
template<class... Ts>
class A {
public:
A(Ts&&... ts) :
t{std::forward<Ts>(ts)...} {}
template <class F>
void visit(F f) {
std::apply([&](auto&&... ts) {
(f(decltype(ts)(ts)), ...);
}, t);
}
std::tuple<Ts...> t;
};
template<auto>
struct ADLReader
{
friend consteval auto GetType(ADLReader);
};
template<auto Identifier, class U>
struct ADLWriter
{
friend consteval auto GetType(ADLReader<Identifier>) { return U{}; }
};
template<auto Identifier>
using Read = std::remove_pointer_t<decltype(GetType(ADLReader<Identifier>{}))>;
template<auto Identifier=[]{}>
class Wrapper {
public:
template<class... Args, class Tuple=A<Args...>, auto=ADLWriter<Identifier, Tuple*>{}>
Wrapper(Args&&... args)
: erased{ Tuple{std::forward<Args>(args)...} }
{ }
template<class F>
void visit(F&& f) {
std::any_cast<Read<Identifier>&>(erased).visit(std::forward<F>(f));
}
private:
std::any erased;
};
int main() {
Wrapper w1(9, 8.9, std::string("hello"));
Wrapper w2("abc"s,"spring"s);
w1.visit([](auto&& t) {
std::println("{}", t);
});
w2.visit([](auto&& t) {
std::println("Length of {} is {}", t, t.size());
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment