In the D website there is an RPN calculator example presented at the frontend when you enter the website. Since D is my reference for powerful metaprogramming (besides Lisp), I was wonderting how to convert a piece of code from C++ to D. Pieces of code do not need to be identical, but should be similar. I discarded the use of macros:
Partial D Version:
Array!int stack;
void binop(string op)()
{
stack[$ - 2] = mixin("stack[$ - 2] " ~
op ~ " stack[$ - 1]");
stack.removeBack();
writeln(stack[$ - 1]);
}
void process(in char[] token)
{
alias Ops = AliasSeq!("+", "-", "*", "/", "%");
Lswitch:
switch (token)
{
foreach (op; Ops)
{
case op:
binop!op();
break Lswitch;
}
case "=":
writeln(stack[$ - 1]);
stack.removeBack();
break;
default:
stack.insertBack(token.to!int);
break;
}
}
Partial C++ version with the help of some utilities:
template <char Op>
void binop(std::vector<int> & s) {
//Returns std::minus<>, std::plus<>, etc.
using Op_t = opstr_to_func_object_t<Op>;
s[s.size() - 2] = Op_t{}(s[s.size() - 2], s[s.size() - 1]);
s.pop_back();
std::cout << s[s.size() - 1] << '\n';
}
template <char Op>
constexpr char op_name<Op> = std::integral_constant<char, Op>;
void process (std::string const & token, vector<int> & s) {
using namespace std;
constexpr auto ops = make_tuple(op_name<'+'>,
op_name<'-'>,
op_name<'*'>,
op_name<'/'>,
op_name<'%'>);
Lswitch:
switch (token[0]) {
//Cannot expand cases inline
// foreach([&](auto && elem) {
// constexpr auto op_str = decltype(elem)::value
//
// }, Ops{});
case "=":
std::cout << s[s.size() - 1] << '\n';
s.pop_back();
break;
default:
s.push_back(stoi(token));
}
}
I have some questions:
Is there a way to expand code inline and mix it with the environment or expand an optimized switch like the one in D without using macros? In D there is a foreach that expands the code inline for cases.
Is there a way to simulate switch with the help of constexpr for strings?
I used outside functions with template parameters because it seems that lambdas cannot specify template parameters, so I would have to use a verbose function object that captures the stack plus templated operator()(). Is there a way to workaround that and keep the functions process and binop in main?
I think the D version scores pretty high at the metaprogramming area, but would like to know how to do the best in C++.