You are here:

C++/How can i evaluate string expression in if statement?

Advertisement


Question
I want to evaluate string expression in if stmt.
E.g.
int i = 0,j = 2;
char * str = "(i == 0) || (j == 2)";

if(str) /* This should execute code within if condition*/
{
...
}

Answer
You will need to write a mini parse to recognize and evaluate the sequence of characters in the string as an expression belonging to some grammar.

Perhaps use a parser library to do this; for example Spirit which is part of Boost.
http://www.boost.org/doc/libs/1_37_0/libs/spirit/classic/index.html

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


vijayan

Expertise

my primary areas of interest are generic and template metaprogramming, STL, algorithms, design patterns and c++09. i would not answer questions about gui and web programming.

Experience

over 15 years

Education/Credentials
post graduate engineer

©2012 About.com, a part of The New York Times Company. All rights reserved.