我正在尝试构建一个'非中心化的'图形结构,其中节点和边缘本身交叉点。与此相似:圆形模板参考结构。
,尽管我试图支持节点可以容纳多种不同的" Arity"的边缘类型。相互互惠,Edge的许多节点类型。
¿是否有一种方法可以通过通用的编程/元编程而不继承继承?
我尝试的以下方法突出了问题:
#include <iostream>
#include <tuple>
#include <list>
using namespace std;
template<class ...EdgeTypes>
class node
{
public:
std::tuple<std::list<EdgeTypes*>...> edges_;
template<size_t I>
using container_value_type_i = typename std::tuple_element<I, decltype(edges_) >::type::value_type;
template< std::size_t I>
void addEdge(const container_value_type_i<I>& e)
{ std::get<I>(edges_).push_back(e); }
};
template<template<class...> class ...V>
class edge
{
public:
std::tuple<V<edge>*...> vertices_;
// ^^^^
// its forcing that all nodes have the same number of 'edges types' (one)
// and that they are of the same 'arity' as edge object
template<size_t I>
using vertex_type_i = typename std::tuple_element<I, decltype(vertices_) >::type;
template< std::size_t I>
void addNode(const vertex_type_i<I>& e)
{ std::get<I>(vertices_) = e; }
};
int main()
{
edge<node> unary_edge;
edge<node, node> binary_edge;
node<edge<node>> unary_node_of_unary_edges;
unary_node_of_unary_edges.addEdge<0>(&unary_edge);
unary_edge.addNode<0>(&unary_node_of_unary_edges);
node<edge<node, node>> unary_node_of_binary_edges;
unary_node_of_binary_edges.addEdge<0>(&binary_edge);
// This won't compile as edge type of node's edges are not the same as unary_edge
//unary_edge.addNode<0>(unary_node_of_binary_edges);
node<edge<node>, edge<node, node>> binary_node_of_unary_edges_and_binary_edges;
binary_node_of_unary_edges_and_binary_edges.addEdge<0>(&unary_edge);
binary_node_of_unary_edges_and_binary_edges.addEdge<1>(&binary_edge);
// This won't compile as nodes's edges are not all the same type
//unary_edge.addNode<0>(&binary_node_of_unary_edges_and_binary_edges);
return 0;
}
您可以在类型系统中编码图形的整个可变结构,但我怀疑这很有用。
类型擦除始终是一个选项。忽略这一点,您可以将模板用于边缘类型和节点类型,以及最大通道。
template<template<class...>class Z>
struct ztemplate{
template<class...Ts>
using z=Z<Ts...>;
};
template<class zE, class Indexes>
class Node;
using zNode=ztemplate<Node>;
template<class zN, class Indexes, class Count>
class Edge;
using zEdge=ztemplate<Edge>;
template<class zE, std::size_t...Is>
class Node<zE,std::index_sequence<Is...>>{
std::tuple< std::list< zE::template z<zNode, std::index_sequence<Is...>, std::integral_constant<std::size_t,Is>>... > edges;
};
template<class zN, std::size_t...Is, std::size_t A>
class Edge<zN,std::index_sequence<Is...>, std::integral_constant<std::size_t,A>>{
std::array< zN::template z<zEdge, std::index_sequence<Is...>>*, A> nodes;
};
或各种。