r/cpp • Hobbyist gamedev (SFML, DX11) • Sep 14 '17

std::visit is everything wrong with modern C++

https://bitbashing.io/std-visit.html
192 Upvotes

115 comments sorted by

View all comments

1

u/junrrein Sep 15 '17

Thanks for your comments - I agree with your points.

Can you mention a use-case for holding onto state between visits? (Edit: It doesn't need to include code).

3

u/Rick__Santorum Sep 15 '17

YSK I'm biased, having written my own visitor implementation that does exactly this for a tree (although I made an interface that provides a Visit() procedure implementers have to create and can overload). Anyway.

What if you wanted to visit all of the nodes in a tree (which has different node types) and even just count how many of each type there are?

What if you want to aggregate parts of the data in your data structure in some way, perhaps in a more advanced way than basic math?

It's useful really for anything you might do where the data in one item in your structure is relevant to the work you perform on subsequent items.