Boost logo

Boost :

From: David Abrahams (david.abrahams_at_[hidden])
Date: 2001-11-14 18:00:35

----- Original Message -----
From: "Douglas Gregor" <gregod_at_[hidden]>

> As I see it, there are 3 types of function objects that don't need
> memory allocation within Boost.Function:
> - function pointers
> - unbound pointers to member functions
> - "stateless" function objects
> Boost.Function allocates no memory for the first of these (function
> pointers), and the other two are on my TODO list. The last one of these
> to be what you want ("stateless" function objects)

Oh, I'm not sure I mean that they're stateless. What I mean is that they
don't need to survive past the call they're passed into. That's a different
question. For example, here's a specialized thing I kludged up for
boost/python/errors.hpp (as seen in the current CVS state) after realizing
that function wouldn't do what I wanted:


struct object_functor_base
    typedef PyObject* result_type;
    virtual PyObject* operator()() const = 0;
    static void* operator new(std::size_t); // don't allow dynamic
    void operator delete(void*);
    void operator delete(void*, size_t);

template <class T>
struct object_functor : object_functor_base
    object_functor(T const& f)
        : m_f(f)

    PyObject* operator()() const
        return m_f();
    T const& m_f;

// Handles exceptions caught just before returning to Python code.
PyObject* handle_exception_impl(object_functor_base const& f);

template <class T>
PyObject* handle_exception(T const& f)
    return handle_exception_impl(object_functor<T>(f));


You can call handle_exception with an arbitrary function object that has a
"signature" PyObject* f(), and it will work without allocating any memory on
the heap. The bulk of the code (in handle_exception_impl) is not repeated
for each different function object type. handle_exception_impl doesn't store
the function object away anywhere, so it doesn't matter that it receives a

I would have liked to be able to leverage the function library for this

> An alternative that would work right now would be to use a custom

How would that help?


Boost list run by bdawes at, gregod at, cpdaniel at, john at