C++ – How to store state on a node for a visitor pattern

cvisitor

I have an architecture that uses the visitor pattern to implement a number of passes over a tree (an AST as it happens). In one of the passes I need to associate some data with a node (nodeX) and then from some point below it get my data from a reference to the nodeX. I want to do this in a way that doesn't push the implementation of the visitor/pass into the tree nodes.

Is there some neat way to make that work?

Ideas:

  • void* on each node (ugly, not type safe)
  • hash_map<Node,Data> (not as clean as I'd like, and who owns it?)

Best Answer

The hash_map sounds like the best way to me. The visitor would own it.

Related Topic