Boost logo

Boost :

From: Tobias Schwinger (tschwinger_at_[hidden])
Date: 2004-09-17 08:25:36


Thorsten Ottosen wrote:
> Hi,
>
> What is the reason that function_traits is not defined for function objects?
> Is it impossible?
>

Hi Thorsten,

Some weeks ago I uploaded a first proposal for an extension to the Type
Traits library capable of handling (all kinds of) function pointers.

These could be applied to a (pointer to) the operator() member of a
functor type, if it's not a member template (as Doug mentioned earlier).

So it does work for for types of the STL functor templates or alike.

Here is an example:

The headers required to compile this, a preliminary documentation and a
more complete example can be found in the yahoo files section
(function_pointer_traits.zip).

[code]

#include <iostream>
#include <typeinfo>
#include <functional>

#include <boost/type_traits/function_pointer_arity.hpp>
#include <boost/type_traits/function_pointer_result.hpp>

// This is used to feed a type traits class with the type deduced
// from its function argument

template < template <typename> class Trait,
             typename T >
typename Trait<T>::type trait_func (T)
{
   return typename Trait<T>::type ();
}

// Create some functor type
typedef std::less<int> my_functor_type;

int main()
{
   using namespace std;
   using namespace boost;

   // Extract some of operator()'s properties
   // and make some (not very formatted) output

   cout << trait_func <function_pointer_arity>
             ( & my_functor_type::operator() )
        << endl
        << typeid ( trait_func <function_pointer_result>
             ( & my_functor_type::operator() ) ).name()
        << endl;

   return 0;
}

[/code]

In case you are using it, playing with it or just looking at it, any
comments are welcome !

Regards,

Tobias


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