Advertisement
cepxuozab

SimpleBuilder

Mar 19th, 2024
823
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.75 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <variant>
  4. #include <vector>
  5.  
  6. struct Node;
  7. using Value=std::variant<std::nullptr_t,int, std::string, std::vector<Node>>;
  8. struct Node {
  9.     Value value{};
  10. };
  11.  
  12. std::ostream& operator<<(std::ostream& out, Value const& v) {
  13.     if (std::holds_alternative<int>(v)) {
  14.         out << std::get<int>(v);
  15.     } else {
  16.         out << "some text";
  17.     }
  18.     return out;
  19. }
  20.  
  21. auto main() -> int {
  22.     Node root;
  23.     std::vector<Node*> stack{&root};
  24.     int val = 42;
  25.     auto& GetVal = stack.back();
  26.     if (!std::holds_alternative<std::nullptr_t>(GetVal->value)) {
  27.         throw std::logic_error("oi - vei! vse propalo!!");
  28.     }
  29.     GetVal->value = val;
  30.     stack.pop_back();
  31.     std::cout << root.value;
  32. }
  33.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement