Skip to content

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

Comments

Until C++2z rolls out with match support, you can roll your own. Nikolai Wuttke gave a lecture at MeetingCpp 2018 (https://www.youtube.com/watch?v=CELWr9roNno) that presents code that lets you do this:

    // Client visitor code
    match(
      thingy,
      [](const PurpleThingy& ) {std::cout << "purple thingy\n";},
      [](const LittleThingy& ) {std::cout << "little thingy\n";},
      [](const auto& ) {std::cout << "any other type\n";}
    );

Working example: https://coliru.stacked-crooked.com/a/dfed39bc7fcdfb01
AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.