ifyalciner
ifyalciner

Reputation: 1248

Circular Dependency in Template Arguments

Someone suggested here using tuples instead of all public structures. And I found it useful. But my problem is now 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 circular dependency in template parameters. Forward declaration (with the knowledge in my possession) won't work since type Edge is can not be formed without knowing type Node and visa versa.

Obviously I can make one of them struct and be done with it. But it will be ugly to break symmetry in access.

Upvotes: 4

Views: 147

Answers (1)

Vittorio Romeo
Vittorio Romeo

Reputation: 93264

You can use struct and public inheritance to fix your circular dependency issue. Node will become a struct wrapper around an 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;
}; 

Upvotes: 5

Related Questions