If you're like me and couldn't get past the first few slides because it's a terrible reading experience, go to the archive, which shows the whole thing in thumbnails. It makes it little better, even though the thumbnails are in reverse: https://cppcrypt.tumblr.com/archive
Grim C++ Tales from the Crypt: The Visitor Pattern (2017)
11–20 of 64 posts
Re: Grim C++ Tales from the Crypt: The Visitor Pattern (2017)
#12Which implements the pseudocode: https://cppcrypt.tumblr.com/post/168134402897
main {
thingies = [ purpleThingy, littleThingy ]
interactions = [ commentOn, cherish ]
for (interaction in interactions)
for (thing in thingies)
interaction.interact(thing)
}
The author does mention things like "function pointers can be used in simple cases" and "std::variant would avoid the need to overload the method". But the main reason for having the C++ code the way it is is because "you can't dispatch to overloaded methods at runtime".
https://cppcrypt.tumblr.com/post/169439207562 ff.Re: Grim C++ Tales from the Crypt: The Visitor Pattern (2017)
#13 interactor_p->interact(thingy_p)
Would this still be considered the visitor pattern or is the extra layer of indirection important?Re: Grim C++ Tales from the Crypt: The Visitor Pattern (2017)
#14I don't understand why the class instance needs to accept the interaction. Why not just invoke the interaction directly? interactor_p->interact(thingy_p) Would this still be considered the visitor pattern or is the extra layer of indirection important?
Re: Grim C++ Tales from the Crypt: The Visitor Pattern (2017)
#15I don't understand why the class instance needs to accept the interaction. Why not just invoke the interaction directly? interactor_p->interact(thingy_p) Would this still be considered the visitor pattern or is the extra layer of indirection important?
Your code won't compile because interactor::interact is not defined on the base class. That line is needed because the overload resolution is static. So with the macro ACCEPTS, you build the code where the overload is resolved.
Re: Grim C++ Tales from the Crypt: The Visitor Pattern (2017)
#16This example actually makes clear why you'd need to do it this was in C++ at least. Not sure which other languages would need this. It's certainly not pretty. Then again, dispatching twice is not so bad compared to your average Java-style over-engineering.
Re: Grim C++ Tales from the Crypt: The Visitor Pattern (2017)
#17C++ match expressions would make std::variant a really nice alternative. I assume there is already a proposal out?
https://coliru.stacked-crooked.com/a/be5c44281eea8bc4
Then only unfortunate missing piece of the puzzle is that there's no trivial way to create a closure out of this, so it requires a bit more manual work to propagate local state to the visitor.
Re: Grim C++ Tales from the Crypt: The Visitor Pattern (2017)
#18C++ match expressions would make std::variant a really nice alternative. I assume there is already a proposal out?
Re: Grim C++ Tales from the Crypt: The Visitor Pattern (2017)
#19C++ match expressions would make std::variant a really nice alternative. I assume there is already a proposal out?