Boost logo

Boost :

From: David Abrahams (dave_at_[hidden])
Date: 2002-09-22 21:37:16


Once again, quoting the function docs:

a.. A function object f of type F is stateless if it is a function pointer
or if boost::is_stateless<T> is true. The construction of or copy to a
Boost.Function object from a stateless function object will not cause
exceptions to be thrown and will not allocate any storage.

And quoting type_traits definition of is_stateless:

True if T is stateless, meaning that T has no storage and
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^--- excise this, BTW
its constructors and destructors are trivial.

I don't understand how this can work. The function object is still allowed
to detect its address by examining the 'this' pointer:

    struct f;
    f* x = 0;

    struct f {
        void operator()() const {
            assert(x != this);
            if (x != 0) x = this;
        }
    };

    function<void()> f1(f());
    function<void()> f2(f());

    f1(); // sets x;
    f2(); // mustn't assert

You have to pick a 'this' pointer anyway, so I hope you're picking the
beginning of the any_pointer subobject. But then, don't you have to check
to make sure it's aligned properly?

Then I have to ask, why bother with is_stateless?
Why not check to see if the function object:

    a. has alignment compatible with the function object
    b. has size <= sizeof(any_pointer)
    c. has_trivial_copy_constructor

And finally, what's up with any_pointer? A union with constructors? Ya
learn something new every day!
That's really weird, though: it's non-POD, yet the compiler can copy it?
I guess this is another argument for loosening the restrictions on PODs.

-----------------------------------------------------------
           David Abrahams * Boost Consulting
dave_at_[hidden] * http://www.boost-consulting.com


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