declval.qbk 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. [/
  2. / Copyright (c) 2008 Howard Hinnant
  3. / Copyright (c) 2009-20012 Vicente J. Botet Escriba
  4. /
  5. / Distributed under the Boost Software License, Version 1.0. (See accompanying
  6. / file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. /]
  8. [article Declval
  9. [quickbook 1.5]
  10. [authors [Hinnant, Howard]]
  11. [authors [Botet Escriba, Vicente J.]]
  12. [copyright 2008 Howard Hinnant]
  13. [copyright 2009-2012 Vicente J. Botet Escriba]
  14. [license
  15. Distributed under the Boost Software License, Version 1.0.
  16. (See accompanying file LICENSE_1_0.txt or copy at
  17. [@http://www.boost.org/LICENSE_1_0.txt])
  18. ]
  19. ]
  20. [/===============]
  21. [section Overview]
  22. [/===============]
  23. The motivation for `declval` was introduced in [@http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2009/n2958.html#Value N2958:
  24. Moving Swap Forward]. Here follows a rewording of this chapter.
  25. With the provision of decltype, late-specified return types, and default template-arguments for function templates a
  26. new generation of SFINAE patterns will emerge to at least partially compensate the lack of concepts on the C++0x timescale.
  27. Using this technique, it is sometimes necessary to obtain an object of a known type in a non-using context, e.g. given the declaration
  28. template<class T>
  29. T&& declval(); // not used
  30. as part of the function template declaration
  31. template<class To, class From>
  32. decltype(static_cast<To>(declval<From>())) convert(From&&);
  33. or as part of a class template definition
  34. template<class> class result_of;
  35. template<class Fn, class... ArgTypes>
  36. struct result_of<Fn(ArgTypes...)>
  37. {
  38. typedef decltype(declval<Fn>()(declval<ArgTypes>()...)) type;
  39. };
  40. The role of the function template declval() is a transformation of a type T into a value without using or evaluating this function.
  41. The name is supposed to direct the reader's attention to the fact that the expression `declval<T>()` is an lvalue if and only if
  42. T is an lvalue-reference, otherwise an rvalue. To extend the domain of this function we can do a bit better by changing its declaration to
  43. template<class T>
  44. typename std::add_rvalue_reference<T>::type declval(); // not used
  45. which ensures that we can also use cv void as template parameter. The careful reader might have noticed that `declval()`
  46. already exists under the name create() as part of the definition of the semantics of the type trait is_convertible in the C++0x standard.
  47. The provision of a new library component that allows the production of values in unevaluated expressions is considered
  48. important to realize constrained templates in C++0x where concepts are not available.
  49. This extremely light-weight function is expected to be part of the daily tool-box of the C++0x programmer.
  50. [endsect]
  51. [/=================]
  52. [section:reference Reference ]
  53. [/=================]
  54. `#include <boost/utility/declval.hpp>`
  55. namespace boost {
  56. template <typename T>
  57. typename add_rvalue_reference<T>::type declval() noexcept; // as unevaluated operand
  58. } // namespace boost
  59. The library provides the function template declval to simplify the definition of expressions which occur as unevaluated operands.
  60. template <typename T>
  61. typename add_rvalue_reference<T>::type declval();
  62. [*Remarks:] If this function is used, the program is ill-formed.
  63. [*Remarks:] The template parameter T of declval may be an incomplete type.
  64. [*Example:]
  65. template <class To, class From>
  66. decltype(static_cast<To>(declval<From>())) convert(From&&);
  67. Declares a function template convert which only participates in overloading if the type From can be explicitly converted to type To.
  68. [endsect]
  69. [/===============]
  70. [section History]
  71. [/===============]
  72. [heading boost 1.50]
  73. Fixes:
  74. * [@http://svn.boost.org/trac/boost/ticket/6570 #6570] Adding noexcept to boost::declval.
  75. [endsect]