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
2
u/dvd0bvb Feb 14 '26
I am aware of ctad, though it can't be used for function args or class members to my knowledge, happy to be corrected. The third option is probably the closest to what I'd hoped for. Another option might be to scrap the wrapper and use duck typing
Really appreciate you taking the time and the detailed answer.