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.
9
u/aocregacc Feb 14 '26
I don't think that's possible like that. If the types are properly hidden inside the Wrapper, the compiler wouldn't know how to instantiate the call operator of the lambda you're passing in. So there has to be some way to specify the types again at the visit call.
5
u/SoerenNissen Feb 14 '26
The compiler needs some way to know what function to call - you either make that statically available to it by encoding it like you do with static_cast, or you need to encode something more than just the
anyso you can do the decoding at runtime.1
u/jk_tx Feb 14 '26
Why is the goal to have a non-templated wrapper? Using visitor pattern with std::variant seems like an obvious choice for this. Is your list of tuplish-classes really so large?
1
u/No-Dentist-1645 Feb 14 '26
I agree with the other comment, this seems like a flawed thing to even attempt to do in the first place, a "template-less wrapper that is somehow also aware of how it was instantiated (i.e templated)" is a bit of an oxymoron, you're asking for "thing that does X without {the thing that does X}".
You should instead use the tools already available to you from the standard library, either an std::variant if you can allow templates, or just a tuple encoded into an std::any (with the "type information" stored elsewhere by the accessor) if you really need true "typed erased" generics
1
u/Business_Welcome_870 Feb 14 '26
I know you want
Wrapperto be a non-template, but class template argument deduction will make it so you don't have to specify the template arguments: https://godbolt.org/z/61d3565hnDoes this solve your issue?
2
u/Business_Welcome_870 Feb 21 '26 edited Feb 25 '26
Okay, so after painstakingly working on this problem for the past 6 days, I was miraculously able to solve it. I took advantage of an ADL friend/deferred template instantiation trick. In the end we get a fairly clean solution: https://godbolt.org/z/bYcThr9xj
This works, but a big issue that this has is that the
type_tagthat is used to retrieve the tuple type is fixed for each instance ofWrapper. This means that if you create a second instance ofWrapper, it will retrieve the sameA<Args...>type as the first wrapper. To get around this you will have to provide your own tag type. One way, which preventsWrapperfrom being a template, is to accept the tag via the constructor andvisit()parameters: https://godbolt.org/z/53fhGdvcYIf you don't want to manually create a new tag type and provide it every time you create an instance and call visit, the other option is to make
Wrapperinto a template that takes its tag by default parameter: https://godbolt.org/z/Ke1fMW7qaNow the tag will be unique for every instance of
Wrapper. If at some point you need to create a function takingWrapperas a parameter, it will need to be a function template:This solution still erases the parameter pack, so I believe it should still be acceptable for you.
Let me know what you think.
1
u/ZachVorhies Feb 15 '26
The best i think you can do is use classes and virtual functions
1
u/mredding Feb 16 '26
std::anyhas one principle use case: as a container for callback contexts. Many callbacks will take a void pointer. This is an instance of something the client wants to pass back to themselves when their callback is called.std::anyprovides a type safe wrapper for that.If YOU are going to consume the contents in an any, and you are not the client, then you want a vector of interfaces.