Boost logo

Boost :

From: Aleksey Gurtovoy (agurtovoy_at_[hidden])
Date: 2002-11-09 19:21:33


Douglas Gregor wrote:
> > In other words, can we allow a zero-arity 'function<...>'
> > to treat ordinary values as first-class function objects?
>
> Do we want to? I think not, because boost::function will lose
> its ability to safely replace function pointers.

That's a valid concern, but on the other hand, as a user that doesn't have
any plain function pointers to replace ;), I might be willing to pay that
price for the benefits of the proposed semantics nevertheless - that is, if
'boost::function<>' itself is not planned to support '= 0' syntax. Judging
from the other thread, it is ;), so consider my suggestion withdrawn.

>
> #include your friendly local lambda header and you can do this:
>
> boost::function<int ()> f1 = constant(-1);

Yes, I realize that, but I wanted a cleaner syntax for this.

> Aside from that, I'm not sure it's implementable. We would
> need a way to tell if an arbitrary class type is a function
> object or not. If we can do this, we will have solved the
> is_callable problem and Boost.Function will become very
> useful indeed :)

I would say that this one ought to work:

    #include "boost/static_assert.hpp"

    typedef char (&no_tag)[1];
    typedef char (&yes_tag)[2];

    template < long > struct sink {};

    template< typename T > no_tag is_callable_helper(...);
    template< typename T > yes_tag is_callable_helper(
          sink< sizeof(&T::operator()) >*
        );

    template< typename T >
    struct is_callable
    {
        BOOST_STATIC_CONSTANT(bool
            , value = sizeof(is_callable_helper<T>(0))
                == sizeof(yes_tag)
            );
    };

    struct her {};
    struct my { int operator()(); };

    BOOST_STATIC_ASSERT(!is_callable<int>::value);
    BOOST_STATIC_ASSERT(!is_callable<her>::value);
    BOOST_STATIC_ASSERT(is_callable<my>::value);

But of course none of the compilers are particularly happy about it. How
about adding 'is_callable<>' to the type_traits proposal and requesting a
compiler support for it?

Aleksey


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