Traversing trees at compile time with C++17 Variadic Templates

I’m currently looking into using C++ (C++17) variadic templates for generating efficient, real-time simulations of circuits.

My goal is to leverage variadic templates to define a tree that can be traversed at compile-time. To define such a tree, I use the following three structs:

template <auto Tag> struct Leaf
{
    static constexpr auto tag = Tag;
};

template <typename ... Children> struct Branch
{
    static constexpr auto child_count = sizeof ... (Children);
    
    template <typename Lambda> void for_each_child(Lambda && lambda)
    {
        // TODO: Execute 'lambda' on each child.
    }
    
    std::tuple<Children ...> m_children {};
};

template <typename Root> struct Tree
{
    template <auto Tag> auto get_leaf()
    {
        // TODO: Traverse the tree and find the leaf with tag 'Tag'.
        
        // If there's no leaf with tag 'Tag' the program shouldn't compile.
    }
    
    Root root {};
};

Using the above definition of a tree, we can define a set of circuit components as follows:

template <auto Tag> struct Resistor : Leaf<Tag>
{
    float resistance() { return m_resistance; }
    
    float m_resistance {};
};

template <auto Tag> struct Capacitor : Leaf<Tag>
{
    float resistance() { return 0.0f; }
    
    float m_capacitance {};
};

template <typename ... Children> struct Series : Branch<Children ...>
{
    using Branch<Children ...>::for_each_child;
    
    float resistance()
    {
        float acc = 0.0f;
        
        for_each_child([&acc](auto child) { acc += child.resistance(); });
        
        return acc;
    }
};

template <typename ... Children> struct Parallel : Branch<Children ...>
{
    using Branch<Children ...>::for_each_child;
    
    float resistance()
    {
        float acc = 0.0f;
        
        for_each_child([&acc](auto child) { acc += 1.0f / child.resistance(); });
        
        return 1.0f / acc;
    }
};

Next, using the above components, we can express a specific circuit like this:

enum { R0, R1, C0, C2 };

using Circuit =
    Tree<
        Parallel<
            Series<
                Resistor<R0>,
                Resistor<C0>
            >, // Series
            Series<
                Resistor<R0>,
                Resistor<R1>
            > // Series
        > // Parallel
    >; // Tree

…where R0, R1, C0, and C1 are tags that we use for accessing components at compile time. E.g. a very basic use case could be the following:

int main()
{
    Circuit circuit {};
    
    circuit.get_leaf<R0>.m_resistance  =  5.0E+3f;
    circuit.get_leaf<C0>.m_capacitance = 10.0E-3f;
    circuit.get_leaf<R1>.m_resistance  =  5.0E+6f;
    circuit.get_leaf<C1>.m_capacitance = 10.0E-6f;
    
    std::cout << circuit.root.resistance() << std::endl;
}

What I just can’t wrap my head around is how to implement the functions for_each_child and get_leaf. I’ve tried different approaches using if-constexpr statements and template-structs without finding a good solution. Variadic templates are interesting but daunting at the same time. Any help would be greatly appreciated.

Source: Windows Questions C++

LEAVE A COMMENT