This Q&A tells that we cannot unstringify something in C/C++. For example:
int i = 1 TO_IDENTIFIER("+") 2;
CANNOT be expanded to
int i = 1 + 2;
So I'm now thinking about an alternative:
Keep a map
that maps the "string literal" to the "operator function" and then invoke the corresponding operator.
But I don't know whether there is a way to put operation function into std::map
as it seems that operator+
is not a unique name as a variable.
Here is the real-world problem I am encountered:
I have a token "$1 + $2" and I can parse them as strings
"$1" "+" "$2"
Then "$1"
and "$2"
are replaced with 2 objects whose type both are z3::expr
. And I also need to replace "+"
with the operation function whose declaration is:
friend z3::expr operator+(z3::expr const & a, z3::expr const & b);
Since there are other operators like -
, <
, I hope this can be done automatically.
Firstly I define a type corresponding to the operator:
typedef z3::expr (*MyOperatorTy)(z3::expr const &a, z3::expr const &b);
Then std::unordered_map
is generated:
with type info in the map:
std::unordered_map<std::string, MyOperatorTy> strOpMap ( {"+", z3::expr operator+(z3::expr const & a, z3::expr const & b)}, ... );
Without type info(just like a normal function variable)
std::unordered_map<std::string, MyOperatorTy> strOpMap ( {"+", operator+}, ... );
Neither works. I also tried std::function<z3::expr(expr const&, z3::expr const)>
as the mapped_type, but still failed.
So is there any way to treat operator functions as a variable and call through function pointers?