2

I wanted to do a recursive evaluation tree for an interpret that returns different types depending on the evaluation in few lines and with no void *so here is a small example of code of what I was trying to do and didn't work

#include <iostream>
using namespace std;

struct node{
  node(string _text){
    child = NULL;
    sibling = NULL;
    text = _text;
  }
  node *child;
  node *sibling;
  string text;
};

auto evaluate(node *n){
  if(n->text == "sum"){
    return (int)evaluate(n->child) + (int)evaluate(n->child->sibling);
  }else if(n->text == "lt"){
    return (int)evaluate(n->child) < (int)evaluate(n->child->sibling);
  }else{
    return atoi(n->text.c_str());
  }
}

int main(){
  node sum = node("sum");
  node a_value = node("23");
  node b_value = node("13");
  sum.child = &a_value;
  sum.child->sibling = &b_value;
  cout << (int)evaluate(&sum) << endl;
}

I would like to know if there is a simple way to do this using modern c++.

6
  • sum.child = &a_value; remember that the nodes go away when the scope of a_value ends.
    – drescherjm
    Commented Mar 16, 2019 at 13:16
  • 2
    Does anything prevent you from using C++'s standard template library (STL)?
    – thb
    Commented Mar 16, 2019 at 13:16
  • 1
    Can you use std::any or std::variant?
    – Eljay
    Commented Mar 16, 2019 at 13:20
  • 2
    Your question is a little ambiguous. Using auto as a return type is allowed only if the compiler can determine the return type at compile time. The compiler does not appreciate being called recursively with an auto return type. As all return types of evaluate are int, by specifying the return type explicitly, the code functions. If this is not your intent, then you will need to do this without auto.
    – Gardener
    Commented Mar 16, 2019 at 13:25
  • @Eljay Yes, but it would need to return a variant. So it's not quite what op wanted, but it's the only way to do it since return types must be determined at compile time.
    – Cruz Jean
    Commented Mar 16, 2019 at 13:35

2 Answers 2

1

evaluate() has 3 cases, where 2 cases return an int value, and one case return a bool. A bool is implicitly convertible to int, so if return type of evaluate() is always int, it works for all cases:

int evaluate(const node* n)
{
    if (n->text == "sum") {
        return evaluate(n->child) + evaluate(n->child->sibling);
    }
    if (n->text == "lt") {
        return evaluate(n->child) < evaluate(n->child->sibling);
    }
    return atoi(n->text.c_str());
}
1
  • yes, thats one of the nifty tricks of c, maybe I've should have posted something more complex, my evaluate function should also return lists and strings so it isn't something I could use.
    – CoderKid
    Commented Mar 16, 2019 at 14:49
1

In C++, auto is determined at compile time. So it will never be a solution for a runtime variation. Another type of construct could be templates, but their type deduction mechanism is compile-time as well.

What you need is runtime type determination. Here a couple of easy solutions:

  • create your own Value class, that allow for value of different basic types to be combined. Polymorphism could help you to implement this type in an elegant and extensible manner.
  • Use an std::variant<...>, for example std::variant<int, double, string>. The value of the variant can be any of the predefined types in the list. This is a kind of type-safe union. The inconvenience is that your evaluation logic will need to check the type of the variant to invoke the right operations (because operations are related to types and have to be known at compile-time).
  • Use an std::any. This is very similar to the variant. It's just more flexible, as you do not need to predefine the types that you'll allow. But technically, for your evaluation logic, you'd still know the possible types to invoke for all possible types and combinations the right operations.

Whatever your choice will be, and not directly related to your main issue, you may be interested:

  • in adding an additional parameter to evaluate() to refer to the evaluation context (e.g. a symbol table, if your expression could also include variables).
  • in the interpreter pattern, which combines and encapsulate node and evaluate().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.