function_traits

| Home | Libraries | People | FAQ | More

### function_traits

    template <class T>
    struct function_traits
    {
       static const std::size_t    arity = see-below;
       typedef see-below           result_type;
       typedef see-below           argN_type;
    };

The class template function_traits will only compile if:

- The compiler supports partial specialization of class templates.
- The template argument `T` is a function type, note that this is not the same thing as a pointer to a function.

| Tip
function_traits is intended to introspect only C++ functions of the form R (), R( A1 ), R ( A1, ... etc. ) and not function pointers or class member functions. To convert a function pointer type to a suitable type use remove_pointer.

Table 1.12. Function Traits Members

Member | Description
--- | ---
`function_traits<T>::arity` | An integral constant expression that gives the number of arguments accepted by the function type `F`.
`function_traits<T>::result_type` | The type returned by function type `F`.
`function_traits<T>::arg`N`_type` | The `N`th argument type of function type `F`, where `1 <= N <= arity` of `F`.

Table 1.13. Examples

Expression | Result
--- | ---
`function_traits<void (void)>::arity` | An integral constant expression that has the value 0.
`function_traits<long (int)>::arity` | An integral constant expression that has the value 1.
`function_traits<long (int, long, double, void*)>::arity` | An integral constant expression that has the value 4.
`function_traits<void (void)>::result_type` | The type `void`.
`function_traits<long (int)>::result_type` | The type `long`.
`function_traits<long (int)>::arg1_type` | The type `int`.
`function_traits<long (int, long, double, void*)>::arg4_type` | The type `void*`.
`function_traits<long (int, long, double, void*)>::arg5_type` | A compiler error: there is no `arg4_type` since there are only three arguments.
`function_traits<long (*)(void)>::arity` | A compiler error: argument type is a function pointer, and not a function type.