2

I have a class called Binary_Node which inherits from an abstract Node class.

class BinaryNode: public Node<BinaryNode>
{
public:
    // Constructor
    BinaryNode(int (*)(int, int));

    // Destructor
    ~BinaryNode();

    // Connectors
    void attach(std::vector<Node *>) override;

    // Function
    int (*function)(int, int);

    // Evaluation
    int evaluate() override;
};

It allows you to attach other binary nodes or to terminal nodes that evaluate to an int. The methods are implemented as follows.

#include "BinaryNode.h"

BinaryNode::BinaryNode(typed_value (*FUNCTION)(typed_value, typed_value)) : Node() {
    function = FUNCTION;
}

BinaryNode::~BinaryNode() = default;

void BinaryNode::attach(std::vector<Node *> CHILDREN) {
    children = CHILDREN;
}

int BinaryNode::evaluate() {
    return function(children[0]->evaluate(), children[1]->evaluate());
}

I recently discovered in C++17 you can do

template<typename ... T>
bool compare(const char scope, T ... args) {
    return ((scope == args) || ...);
}

compare('a', 'b', 'c', 'd') // False
compare('a', 'b', 'c', 'a') // True

Could something similar be done to my class that allows the function to evaluate as many children as supplied. Something like

template<typename ... T>
int BinaryNode::evaluate(T ... args) {
    return function(/*?*/);
}

There by converting it from a Binary_Node class to a Nonterminal_Node class.

Edit:

I have found quite a good example on stack overflow of what I want to do and how to accomplish it. Essentially it's expanding std::vector into a parameter pack. Now I still need to find a workaround for a virtual template member function.

Ivor Denham-Dyson
  • 655
  • 1
  • 5
  • 24

1 Answers1

0

Depends on what evaluation is supposed to do, if you wanna sum the returned values, you can override the + operator and fold over it

Yamahari
  • 1,926
  • 9
  • 25