Boost logo

Boost :

From: Steven Watanabe (steven_at_[hidden])
Date: 2007-12-03 10:52:20


AMDG

Alexander Nasonov <alnsn <at> yandex.ru> writes:

> Wow! I like it. I haven't implemented it yet but the sketch looks absolutely
> correct.

There's a little problem. How to get the arguments from the
opening macro to the closing one without interfering with
another local function. I think I've found a solution for
that, too. The key is that we need to declare a variable
if it has not yet been declared. We can test whether it has
been declared by creating a global with the same name and
using a sizeof expression. So, now we need some syntax
that will be either a declaration or something else depending
on a boolean condition. To do this we can take advantage of
the ambiguity that requires the ::template syntax for dependent
templates.

typedef char no;
struct yes { no dummy[2]; };

template<bool>
struct conditional_declare;

struct undeclared {};

template<>
struct conditional_declare<true> {
    template<int>
    struct apply {
        void* value;
        friend void operator>(bool, const apply&) {}
    };
};

template<>
struct conditional_declare<false> {
    static const int apply = 0;
};

template<class T>
yes is_declared(const T&);
no is_declared(const undeclared&);

undeclared local_function_bound_args;

#define BOOST_LOCAL_FUNCTION_DECLARE_ARGS() \
     conditional_declare<sizeof(is_declared(local_function_bound_args)) == \
     sizeof(no)>::apply<0> local_function_bound_args

int main() {
    BOOST_LOCAL_FUNCTION_DECLARE_ARGS(); // declares local_function_bound_args
    BOOST_LOCAL_FUNCTION_DECLARE_ARGS(); // turns into a pair of comparisons
}

In Christ,
Steven Watanabe


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk