Circular dependency in template arguments

Someone suggested here using tuples instead of all public structures. And I found this helpful. But now my problem is with the following section:

using Edge = std::tuple<Node_wp,//From Node
                        Node_wp>;//To Node
using Edge_wp = std::weak_ptr<Edge>;

using Node = std::tuple<std::vector<Edge_wp>,//Incoming Edges
                        std::vector<Edge_wp>>;//Outgoing Edges
using Node_wp = std::weak_ptr<Node>;

How can I overcome this cyclic dependency in the template options. An advanced declaration (with the knowledge at my disposal) will not work, because the Edge type cannot be formed without knowledge of the Node type and vice versa.

Obviously, I can do one of them structand do with it. But it will be ugly to break access symmetry.

+4
source share
1 answer

struct , . Node struct std::tuple:

#include <memory>
#include <tuple>
#include <vector>

struct Node;
using Node_wp = std::weak_ptr<Node>;

using Edge = std::tuple<Node_wp,  // From Node
                        Node_wp>; // To Node
using Edge_wp = std::weak_ptr<Edge>;

struct Node : std::tuple<std::vector<Edge_wp>, // Incoming Edges
                         std::vector<Edge_wp>> // Outgoing Edges
{
    using std::tuple<std::vector<Edge_wp>, std::vector<Edge_wp>>::tuple;
}; 
+5

Source: https://habr.com/ru/post/1687544/


All Articles