|
Boost : |
From: Alexander Nasonov (alnsn_at_[hidden])
Date: 2007-08-22 04:21:23
Steven Watanabe <watanabesj <at> gmail.com> writes:
> Also, I need to be able to specify the name of the function somehow
> and I am not sure how this can be accomplished with that interface.
> Here's what I have right now:
>
> ...
>
> So that
>
> int BOOST_LOCAL_FUNCTION(name, (i)(j), (int x)(int y)) {
> //body here
> } BOOST_LOCAL_FUNCTION_END
>
> expands to
>
> int result_tag;
> typedef BOOST_TYPEOF(result_tag) result_type;
> typedef BOOST_TYPEOF(i) type_0;
> typedef BOOST_TYPEOF(j) type_1;
> typedef boost::tuple<type_0, type_1> tuple_type;
> typedef local_function<tuple_type, result_type(tuple_type, int x, int
> y)> function_type;
> function_type name(make_tuple(i, j));
> {
> function_type* function_ptr = &name;
> struct body {
> result_type impl(tuple_type& args, int x, int y) {
> type_0& i = boost::get<0>(args);
> type_1 j = boost::get<1>(args);
> {
> //body here
> }
> }
> };
> function_ptr->f = &body::impl;
> }
This is really impressive!
I'd moved function arguments closer to the function name
int BOOST_LOCAL_FUNCTION(name, (int x)(int y), (i)(j) )
Ideally, arguments should be listed naturally:
int BOOST_LOCAL_FUNCTION(name, (int x, int y), (i)(j) )
but I don't see how to pass the args variable to body::impl.
> Note that i and j are passed by value rather than by reference. Otherwise,
> it is not safe to have a copy of the function around after control
> leaves the scope
> it is defined in.
Passing by reference is often fine for ScopeExit but it's very dangerous
for local functions. Optionally passing by reference would be a nice
feature, though.
-- Alexander
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk