簡體   English   中英

C ++關於本身嵌入結構名稱的困惑

[英]c++ confusion about embedding a struct name in itself

我正在學習boost,這是來自以下代碼的一段: http : //www.boost.org/doc/libs/1_55_0/libs/graph/example/visitor.cpp我對struct edge_printer的定義確實感到困惑,它使用base_visitor的繼承,但是base_visitor模板的類型名稱指定為edge_printer本身。 請問這在C ++中是什么?

template <class Tag>
struct edge_printer : public base_visitor<edge_printer<Tag> > {
  typedef Tag event_filter;
  edge_printer(std::string edge_t) : m_edge_type(edge_t) { }
  template <class Edge, class Graph>
  void operator()(Edge e, Graph& G) {
    std::cout << m_edge_type << ": " << source(e, G) 
              << " --> " <<  target(e, G) << std::endl;
  }
  std::string m_edge_type;
};
template <class Tag>
edge_printer<Tag> print_edge(std::string type, Tag) { 
  return edge_printer<Tag>(type);
}

這稱為混合。 參見CRTP有關mixins的文章

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM