Boost logo

Boost Users :

From: David Abrahams (dave_at_[hidden])
Date: 2004-12-27 13:39:42


Hossein Haeri wrote:
> Dear all,
>
> Yep, again it's me, and it's agian
> function_traits<>...
>
> AFAIK, std::iterator_traits<> is one of the
> counterparts of function_traits<>, not?
>
> A sort of symmetry which iterator_traits<> has got is
> that it can work for Iterators as well as plain
> pointers.

That's the wrong way to think about it. It just works for iterators.
Plain pointers just happen to be iterators, by the *standard's
definition* of "iterator."

> This seems to be not the case for function_traits<>.
> That is, you can pass a function type to it, but you
> can't do that for a functor. Is that right?

That's because function object types (what you're calling functors) are
not functions by the standard's definition of "function."

> If it is, a big drawback turns out to happen for
> variations of the following which are not that few
> common:
>
> template <
> typename ...,
> ...,
> typename Function
> >
> struct Foo
> {
> typedef
> typename
> function_traits<Function>
> ::result_type result_type;
>
> Foo (Function f) : f_(f) {}
>
> //...
> result_type operator () () {/*..*/}
>
> Function f_;
> };

It doesn't work because there's no way to code it in general. First of
all there's no universal standard for a way to get the result type of a
function object. Accessing a nested result_type sometimes works, but
then, sometimes the operator() is overloaded, or even templated.

You might look at boost::result_of for a more general solution.

-- 
Dave Abrahams
Boost Consulting
http://www.boost-consulting.com

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net