This could be spelled more ergonomically as:
variant_.dynamic_emplace(deserialized_index, [](auto *p) {
std::construct_at(p); deserialize(*p, stream);
});
where you just pass in the index you want to activate, and then the variant calls your visitor with an appropriate `T*` pointer for you to construct into.
In other words:
std::variant<int, std::string> v;
v.emplace<1>("hello"); // legal in C++17, but requires a compile-time-constant template argument 1
v.dynamic_emplace(1, [](auto *p) {
if constexpr (std::same_as<decltype(p), std::string*>) {
std::construct_at(p, "hello");
}
}); // silly, but legal, in C++Fantasy; permits a run-time-computed function argument 1
Your utility to turn a runtime value into a compile-time integral_constant is certainly more general, but also vastly more expensive (one line of code could blow up into 256 or 65536 instantiations of that generic lambda's operator() template!) and I don't immediately see any really killer app for it. If you need this only for `variant`, then it seems like the generality isn't needed.
my $.02,
Arthur