daydayup
daydayup

Reputation: 2317

c++ confusion about embedding a struct name in itself

I am learning boost and here is a piece of code from :http://www.boost.org/doc/libs/1_55_0/libs/graph/example/visitor.cpp I am really confused in the definition of struct edge_printer, it uses a inheritance of base_visitor, but the type name of base_visitor templates is specified as edge_printer itself. Can I ask what this is called in 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);
}

Upvotes: 0

Views: 75

Answers (1)

Rudolfs Bundulis
Rudolfs Bundulis

Reputation: 11944

This is called mix-in. See CRTP and this post on mixins.

Upvotes: 1

Related Questions