File tree Expand file tree Collapse file tree 1 file changed +30
-0
lines changed
Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Original file line number Diff line number Diff line change @@ -689,6 +689,34 @@ void visitor_demo()
689689 }
690690}
691691
692+ // std::visit demo:
693+
694+ static stringstream output;
695+
696+ struct Visitor_op {
697+ void operator ()(int arg) { output << " int " ; };
698+ void operator ()(long arg) { output << " long " ; };
699+ void operator ()(double arg) { output << " double " ; };
700+ void operator ()(string arg) { output << " string " ; };
701+ };
702+
703+ void visitor_std_demo ()
704+ // / Demonstration of standard template [visit](https://en.cppreference.com/w/cpp/utility/variant/visit)
705+ {
706+ // trivial visit call
707+ visit ([](auto && arg) {}, variant<int > { 1 });
708+
709+ // practical visit usage
710+ using Components = variant<int , long , double , string>;
711+ vector<Components> comps = { 10 , 15l , 1.5 , " hello" };
712+ Visitor_op visitors;
713+
714+ for (auto & c : comps)
715+ visit (visitors, c);
716+
717+ assert (output.str () == " int long double string " );
718+ }
719+
692720// / @} visitor
693721
694722struct Handler
@@ -744,6 +772,8 @@ void behavioral_patterns_demo()
744772 chain.handle (cmnd);
745773
746774 visitor_demo ();
775+
776+ visitor_std_demo ();
747777}
748778
749779// / @} BP
You can’t perform that action at this time.
0 commit comments