|
Boost : |
From: Douglas Gregor (gregod_at_[hidden])
Date: 2002-05-12 11:19:48
On Sunday 12 May 2002 07:52 am, you wrote:
> > function object traits to work with.
> >
> > If we _did_ have a working set of function traits, it would be possible
> > to make Boost.Function objects implicitly constructed only from function
>
> objects
>
> > that are callable by the Boost.Function object.
>
> This has nothing to do with my original problem, but I'm curious what this
> Boost.Function constructor would look like if there was a working
> boost::arity_traits class. I'm relatively new to generic programming using
> C++ templates and I'd like to learn more.
>
> Thanks in advance for teaching me a new trick. :)
>
> Dirk Gerrits
I shouldn't have said "it would be possible" but been a little more cautious
and said "it might be possible". There are a few tricks that might be usable,
e.g., the has_member idea (it's in the archives) could probably be applied
like this:
template<bool Cond> struct yes
{
typedef void* type;
};
template<> struct yes<false>
{
// no type typedef
};
template<typename R, typename T1, ..., typename TN>
class function {
public:
template<typename F>
function(F f,
typename yes<is_callable<F, R, T1, ..., TN>::value>::type = 0);
// ...
};
The idea being that if F is callable, yes<true>::type will be void* and
everything is okay. However, if F is not callable then yes<true>::type does
not even exist so the constructor cannot be instantiated.
This worked in a little proof-of-concept test.
The next step is to work on is_callable. I know that the following cases can
be handled:
- function pointers
- member function pointers
- anything derived from std::unary_function or std::binary_function
- anything that has function object traits and is not partially bound
I don't know if is_callable can be written to handle arbitrary function
objects.
Doug
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk