Several coworkers and I have been working on a compiler project for the last six months or so. We're implementing it in C++, and, for most of us, it's our first real C++ project. We're using various flavors of visitors1 for a lot of our IR processing. In quite a few places we have visitors that just do stuff but have no value. To invoke them some code that looks something like:
ir_validate_tree v; v.run(instructions);
Often these are wrapped in a function. In other places we have things like:
ir_swizzle_swizzle_visitor v; v.run(instructions); /* Do something with v.progress */
The latter form is always wrapped in a function that just returns
I find the first form to be particularly ugly. Wrapping it in a function makes using it less ugly, but the ugly is still there, lurking in the shadows.
It occured to me today that at least the first form could be "fixed" by using
the constructor as the
run method. Instead of the code above, there would
be a constructor that had the code from the
run method, and callers would
Something similar could be done with the second case, but that would look like:
progress = ir_swizzle_swizzle_visitor(instructions).progress || progress;
My conundrum, as a C++ newb, is whether or not this is a common idiom. Would someone familiar with C++ (at least more familiar than I am) look at that code and know what was going on? Or would they just think the author was a clueless newb?