TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Grim C++ Tales from the Crypt: The Visitor Pattern (2017)

114 点作者 meebob大约 6 年前

18 条评论

grandinj大约 6 年前
The thing that annoys me most about this pattern is that people always miss the &quot;there needs to be lots of things you want to perform on those thingies&quot;.<p>And so we end up with a huge pile of visitor code, and there is one or maybe two actual visitors.<p>When it would have been far easier to read and modify if it was just coded out normally.
评论 #19319292 未加载
评论 #19319290 未加载
评论 #19319906 未加载
Alterlife大约 6 年前
If you&#x27;re like me and couldn&#x27;t get past the first few slides because it&#x27;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:<p><a href="https:&#x2F;&#x2F;cppcrypt.tumblr.com&#x2F;archive" rel="nofollow">https:&#x2F;&#x2F;cppcrypt.tumblr.com&#x2F;archive</a>
评论 #19319165 未加载
评论 #19319373 未加载
hathawsh大约 6 年前
The visitor pattern isn&#x27;t appropriate everywhere, but it&#x27;s quite good for traversing and manipulating ASTs (abstract syntax trees). An AST typically has many kinds of nodes, but programs that traverse the AST usually only care about some small subset of those nodes. The visitor pattern provides a clean solution: a generic AST traversal library visits all nodes in the tree while allowing the program to customize what happens when visiting specific nodes. I&#x27;ve found the visitor pattern very effective for creating derivative ASTs without knowing about everything that might be in the AST.
评论 #19321407 未加载
评论 #19321742 未加载
drmeister大约 6 年前
It&#x27;s things like the visitor pattern that led me to implement Clasp - a Common Lisp that interoperates with C++ and uses llvm as the backend (github.com&#x2F;clasp-developers&#x2F;clasp.git). Common Lisp has generic functions and multiple dispatch - so it doesn&#x27;t need the wretched visitor pattern. Try writing a compiler using the visitor pattern - I dare you. :-)
评论 #19321010 未加载
mcv大约 6 年前
This... I can actually see the point in this. The Visitor pattern is one of those patterns I never really saw the point of, and which mostly struck me as an over-engineered hack about a shortcoming in a language.<p>This example actually makes clear why you&#x27;d need to do it this was in C++ at least. Not sure which other languages would need this. It&#x27;s certainly not pretty. Then again, dispatching twice is not so bad compared to your average Java-style over-engineering.
评论 #19320247 未加载
评论 #19319872 未加载
评论 #19324713 未加载
laythea大约 6 年前
I clicked the link and then tumblr asked me about privacy, with a link to &quot;Manage options&quot;.<p>I clicked the link, hoping to disable x,y,z and there is just text. No options!<p>Not reading further.
slacka大约 6 年前
What a terrible UI on that website. Constantly clicking &quot;Next&quot;! Yuk. And every page required zooming in and out to make it fit on my screen. For a tech focused comic, you&#x27;d think the author could run it through an ImageMagick batch resize job. Bonus points for allowing keyboard back and forth navigation.
评论 #19322918 未加载
评论 #19322334 未加载
评论 #19321248 未加载
doctorRetro大约 6 年前
While I, as a rule, try not to stand against good storytelling, I clicked far too many times while thinking &quot;get to the point&quot; before I just closed the tab.
rgoulter大约 6 年前
The example code the author comes up with: <a href="https:&#x2F;&#x2F;github.com&#x2F;dpugson&#x2F;examples&#x2F;blob&#x2F;master&#x2F;chpt1_the_visitor_pattern&#x2F;example.cpp" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;dpugson&#x2F;examples&#x2F;blob&#x2F;master&#x2F;chpt1_the_vi...</a><p>Which implements the pseudocode: <a href="https:&#x2F;&#x2F;cppcrypt.tumblr.com&#x2F;post&#x2F;168134402897" rel="nofollow">https:&#x2F;&#x2F;cppcrypt.tumblr.com&#x2F;post&#x2F;168134402897</a><p><pre><code> main { thingies = [ purpleThingy, littleThingy ] interactions = [ commentOn, cherish ] for (interaction in interactions) for (thing in thingies) interaction.interact(thing) } </code></pre> The author does mention things like &quot;function pointers can be used in simple cases&quot; and &quot;std::variant would avoid the need to overload the method&quot;. But the main reason for having the C++ code the way it is is because &quot;you can&#x27;t dispatch to overloaded methods at runtime&quot;. <a href="https:&#x2F;&#x2F;cppcrypt.tumblr.com&#x2F;post&#x2F;169439207562" rel="nofollow">https:&#x2F;&#x2F;cppcrypt.tumblr.com&#x2F;post&#x2F;169439207562</a> ff.
choeger大约 6 年前
The thing with the visitor pattern is that it indeed is useful for many operations over fixed data. But the same holds for simple pattern matching. Inheritance (or rather subtyping) is useful for changing data but a fixed set of operations. But what the eff do we do when we have many operations on changing data? The expression problem still is an interesting problem, because a data structure that solves it would basically be the &quot;gold standard&quot;.
deckar01大约 6 年前
I don&#x27;t understand why the class instance needs to accept the interaction. Why not just invoke the interaction directly?<p><pre><code> interactor_p-&gt;interact(thingy_p) </code></pre> Would this still be considered the visitor pattern or is the extra layer of indirection important?
评论 #19319439 未加载
评论 #19329751 未加载
adgasf大约 6 年前
C++ match expressions would make std::variant a really nice alternative.<p>I assume there is already a proposal out?
评论 #19319720 未加载
评论 #19319831 未加载
评论 #19320141 未加载
评论 #19319691 未加载
zwieback大约 6 年前
When I originally read about Visitor in the GoF book it was fun to work through its convoluted way but seemed ugly in any language, including the original SmallTalk.<p>I&#x27;d probably just do type checks and maybe use a 2D table for the possible interactions.
dysoco大约 6 年前
I enjoyed the format of this, kept me interested. Nowadays I feel like if I read a blog post I have such a short attention span I&#x27;d just glance over it.<p>Would love to see more stories.
ryanmarsh大约 6 年前
The only time I have ever encountered a code base where the visitor pattern was necessary and fulfilling its purpose is Babel (the JS compiler).
评论 #19321368 未加载
malka大约 6 年前
their GDPR compliance screen is full of anti patterns. Will not read.
评论 #19319925 未加载
satellitec4t大约 6 年前
How the &amp;$#! do I read this
jstimpfle大约 6 年前
If you ask me, the actual problem is the tree data structure... Maybe post order forms lend themselves better to processing.<p>The even deeper problem is what ASTs are meant to <i>represent</i>, to which the answer would be &quot;possibly a lot of diverse things&quot;. Diversity is never good in a computational context. But I don&#x27;t see a good way to avoid it in the context of programming languages and ASTs. The reason for the diversity is that programming languages should allow humans to specify what should happen in very few keystrokes.