Circular template reference structure

会有一股神秘感。 提交于 2019-12-12 01:29:09

问题


I have a ploblem about circular template reference. I want to make a tree using class node and class edge as following;

template <typename EdgeT>
class node
{
public:
    std::vector<EdgeT> edge_out;
    std::vector<EdgeT> edge_in;
};


template <typename NodeT>
class edge
{
public:
    NodeT* src;
    NodeT* dst;
    int weight;
};


template <typename NodeT, typename EdgeT>
class graph
{
public:
    std::vector<NodeT> nodes;
};

I found that I cannot declare graph class ex:

graph< node, edge > g; // <--- this cannot be solved 

graph< node< edge <node.....>, edge< node< edge>>  >  //it makes infinity declaration..

How can I redefine the structure of the classes?


回答1:


Here is one approach:

#include <vector>

template<template<typename NodeT,typename T>class EdgeT, typename T=double>
struct Node {
   typedef Node<EdgeT,T> self_type;
   typedef EdgeT<self_type, T> edge_type;
   std::vector<edge_type> edge_out;
   std::vector<edge_type> edge_in;
   T data;
};

template<typename NodeT,typename T>
struct Edge {
   typedef NodeT node_type;
   node_type* src;
   node_type* dst;
   int weight;
};

template<typename NodeT, typename EdgeT=typename NodeT::edge_type>
struct graph {
   typedef NodeT node_type;
   typedef EdgeT edge_type;
   std::vector<NodeT> nodes;
};

int main() {
   typedef graph< Node<Edge> > graph_type;
   graph_type my_graph;
   my_graph.nodes.push_back( graph_type::node_type() );
   my_graph.nodes.push_back( graph_type::node_type() );
   my_graph.nodes.front().edge_out.push_back( {&my_graph.nodes[0], &my_graph.nodes[1], 1} );
   my_graph.nodes.back().edge_in.push_back( {&my_graph.nodes[0], &my_graph.nodes[1], 1} );
}

for another approach, you could look at how boost::variant handles recursive variants.

Another way to approach this would be more formal. C++ template metaprogramming is a functional language -- there are various techniques from functional programming in order to describe recursive structures without forward declaration that can be used.

I'm betting a fixed point combinator of some kind might work, but it is beyond me to figure out how. :)




回答2:


You need to figure out the reason why you need to use templates. If for example you want to have the data dynamic in an edge, you could use:

//foward declaration
template <typename T>
class node
{
std::vector<Edge<T> > edge_out;
std::vector<Edge<T> > edge_in; 
} 


template <typename T>
class edge
{
Node<T>* src;
Node<T>* dst;
T    weight
}


template <typename T>
class graph
{
std::vector<Node<T> > nodes;
}

Likewise if you want to have data that differs in your node. But in general it is better to figure out the reason for templates beforehand. I have looked at overly templatized production code and it is quite unmaintainable.



来源:https://stackoverflow.com/questions/15330073/circular-template-reference-structure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!