r/cpp_questions • u/dvd0bvb • Feb 14 '26
SOLVED Visitor Pattern Using std::any
I'm attempting to write a type erased wrapper around a sort of tuple-like class. The wrapper holds a std::any where the tuple-ish class is stored. I want to implement a visit method where the wrapper takes some arbitrary callable which fills a similar role to std::apply. I can't seem to find a way to get the type of the object stored in the std::any in the same place as the function to apply.
I have a (hopefully) clarifying example here https://godbolt.org/z/xqd1q8sGc I would like to be able to remove the static_cast from line 47 as the types held in the tuple-like class are arbitrary.
I'm open to other ideas or approaches. The goal is to have a non-templated wrapper class for a container of unrelated types.
View all comments
Show parent comment
1
u/dvd0bvb Feb 14 '26
I typed this out in response to another comment but it was apparently deleted before I posted.
The actual use case is an audio pipeline which contains de/encoders, filters, resamplers, whatever the use case. The underlying Pipeline<...> has access to all those types so adding this functionality is trivial, I added it to the A class in the godbolt example. This visiting functionality allows for modifying a set of elements in the pipeline.
I wanted type erasure purely for ease of use. Pass a PipelineWrapper instead of a Pipeline<...> which propagates the template params to consumers.