[lambda] extract a constant subexpression by introspection

Hello, I have a lambda expression that is suppoalways constructed as [some lambda expression] / ( _1 - c); where c is a double constant, for example c = 3.14. What I need is a function that given the expression returns the internal value of c, if that is possible: template<class LambdaExp> double c(LambdaExp expr){ ... return c in expr assuming that expr has the form [...]/(_1 - c) } The function may throw an exception or whatever if the expr is not of the form [...]/(_1 - c) Some additional infomation that maybe useful to simplify the solution: the lambda expression returns a double, _1 is meant to be a placeholder for double. optional feature: Also it would be ideal if the function could also recognize *simple* alternative expressions like [..]/(_1 + k) (and return -k) or [...]/( k + _1) (and return -k). Solutions using other lambda-like libraries are welcomed too. Thank you, Alfredo

On May 21, 6:21 pm, alfC <alfredo.cor...@gmail.com> wrote:
I have a lambda expression that is suppoalways constructed as [some lambda expression] / ( _1 - c); What I need is a function that given the expression returns the internal value of c, if that is possible:
Answering to myself, hope it is useful to someone else: It turns out that extracting c from the lambda expression f_expr is quite easy. just boost::tuples::get<1>(boost::tuples::get<1>(f_expr.args).args), The tricky part is to write the argument type of the function that extracts 'c' only in the case that the expression if of type 'XX/(_1- c)' Below is the full function that has to be defined to extract c, if the expression is not of type XX/(_1-c) the function is not even instantiated: template<class LambdaExp> double extract_c( //typename simple_rational_function<LambdaExp>::type const& // doing a convenient typedef doesn't work, at least for gcc lambda_functor<lambda_functor_base< arithmetic_action<divide_action>, tuple< lambda_functor<// LambdaExp >,// lambda_functor<lambda_functor_base< arithmetic_action<minus_action>, tuple< lambda_functor< placeholder<1> >, double const // <-- attention adds const to avoid unreadable errors > > > > > > f_expr){ return boost::tuples::get<1>(boost::tuples::get<1>(f_expr.args).args); } Suggestions accepted, Alfredo
participants (1)
-
alfC