tour cpp: std::variant 實現無繼承層次的訪問者模式

joel-q發表於2024-10-11

std::variant 是基於模板而實現的一種包括了一個標誌位的高階union物件;可以完全替代如下場景:

struct st {
    int type;
    union un {
        int i;
        float f;
    };
};
#include <iostream>
#include <variant>

template <class... base>
struct overloaded : base... {
  using base::operator()...;
};
template <class... base>
overloaded(base...) -> overloaded<base...>;

// struct checker;
struct Declaration {
  // void accept(checker *ckr) {
  //     std::cout << "Declaration accept it" << std::endl;
  // }
  int declaration_type{0};
};
struct Expression {};
struct Statement {};

using Node = std::variant<Declaration *, Expression *, Statement *>;
struct checker {
  void visit(Node node);
};

void checker::visit(Node node) {
  Declaration *declaration{};
  if (std::holds_alternative<Declaration *>(node) and
      (declaration = std::get<Declaration *>(node))) {
    std::cout << "declaration_type: " << declaration->declaration_type
              << std::endl;
  }  // else if ...
}

int main() {
  checker ckr;
  Declaration declaration{.declaration_type = 2};
  Node node{&declaration};
  ckr.visit(node);
  return 0;
}

相關文章