|
Boost : |
From: Atry (pop.atry_at_[hidden])
Date: 2007-06-28 23:04:22
There is an sample for static_lambda use of static variant:
/* begin */
int a = 5;
int sub(int l, int r)
{
return l - r;
}
BOOST_STATIC_LAMBDA_DECLARE_EXPRESSION(int, expr_a, a);
// equals to BOOST_STATIC_LAMBDA_DECLARE_EXPRESSION(BOOST_TYPEOF(&sub),
expr_sub, &sub)
BOOST_STATIC_LAMBDA_AUTO_EXPRESSION(expr_sub, &sub);
int main()
{
BOOST_ASSERT(to_functor(expr_sub(constant<boost::mpl::int_<62> >(),
expr_a)).static_invoke() == sub(62, a));
a = 10;
BOOST_ASSERT(to_functor(expr_sub(constant<boost::mpl::int_<62> >(),
expr_a))() == sub(62, a));
}
/* end */
Any static variant be used must register first, because I can't get a
variant value in compile time.
Another way is using constant<>, like constant<boost::mpl::int_<62> >().
On 6/29/07, Alexander Nasonov <alnsn_at_[hidden]> wrote:
>
> Daniel Walker wrote:
> > Right. Actually, now that I think about it I'm getting a little
> > confused about scenarios where this could save you space. I think they
> > may be limited.
>
> Scenarios for static stateless lambda expressions are limited, so it's
> not a surprise that scenarios where this could save you space are
> limited too ;)
>
> We should carefully think about static _stateful_ lambda expressions.
> Some lambda expressions contain only constants and these expressions can
> be
> "complied" into static function. Though, it may lead to a confusion when
> a user pass a variable (not constant!) but static lambda uses a value
> passed first time.
>
> int x = 0;
> (_1 + x).get_static_function<int>()(0); // returns 0 - ok
> x = 1;
> (_1 + x).get_static_function<int>()(0); // returns 0 - bad
>
> Too bad.
> I have another use-case where stateful lambda could be useful but
> above problem doesn't give it any real chance :-(
>
> --
> Alexander Nasonov - http://nasonov.blogspot.com
>
> Pennies do not come from heaven. They have to be earned here on
> earth. -- Margaret Thatcher --
> /---------------------------- Fortune script ----------------------------\
> curl 'http://www.quotedb.com/quote/quote.php?action=random_quote&=&=&' | \
> sed -e "s/^[^']*'//;s/.);$/ --/;s/<[^>]*>//g;2s/^.\{17\}//" |
> fmt
> _______________________________________________
> Unsubscribe & other changes:
> http://lists.boost.org/mailman/listinfo.cgi/boost
>
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk