function_traits.qbk 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. [/
  2. Copyright 2007 John Maddock.
  3. Distributed under the Boost Software License, Version 1.0.
  4. (See accompanying file LICENSE_1_0.txt or copy at
  5. http://www.boost.org/LICENSE_1_0.txt).
  6. ]
  7. [section:function_traits function_traits]
  8. [def __argN '''arg<replaceable>N</replaceable>_type''']
  9. template <class F>
  10. struct function_traits
  11. {
  12. static const std::size_t arity = __below;
  13. typedef __below result_type;
  14. typedef __below __argN;
  15. };
  16. The class template function_traits will only compile if:
  17. * The compiler supports partial specialization of class templates.
  18. * The template argument `F` is a /function type/, note that this ['[*is not]]
  19. the same thing as a /pointer to a function/.
  20. [tip
  21. function_traits is intended to introspect only C++ functions of the
  22. form R (), R( A1 ), R ( A1, ... etc. ) and not function pointers or
  23. class member functions. To convert a function pointer type to a suitable
  24. type use __remove_pointer.]
  25. [table Function Traits Members
  26. [[Member] [Description]]
  27. [[`function_traits<F>::arity`]
  28. [An integral constant expression that gives the number of arguments accepted by the function type `F`.]]
  29. [[`function_traits<F>::result_type`]
  30. [The type returned by function type `F`.]]
  31. [[`function_traits<F>::__argN`]
  32. [The '''<replaceable>N</replaceable>th''' argument type of function type `F`, where `1 <= N <= arity` of `F`.]]
  33. ]
  34. [table Examples
  35. [[Expression] [Result]]
  36. [[`function_traits<void (void)>::arity`] [An integral constant expression that has the value 0.]]
  37. [[`function_traits<long (int)>::arity`] [An integral constant expression that has the value 1.]]
  38. [[`function_traits<long (int, long, double, void*)>::arity`] [An integral constant expression that has the value 4.]]
  39. [[`function_traits<void (void)>::result_type`] [The type `void`.]]
  40. [[`function_traits<long (int)>::result_type`] [The type `long`.]]
  41. [[`function_traits<long (int)>::arg1_type`] [The type `int`.]]
  42. [[`function_traits<long (int, long, double, void*)>::arg4_type`] [The type `void*`.]]
  43. [[`function_traits<long (int, long, double, void*)>::arg5_type`] [A compiler error: there is no `arg5_type` since there are only four arguments.]]
  44. [[`function_traits<long (*)(void)>::arity`] [A compiler error: argument type is a /function pointer/, and not a /function type/.]]
  45. ]
  46. [all_compilers]
  47. [endsect]