detected_or.qbk 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. [/
  2. Copyright 2018 Glen Joseph Fernandes
  3. (glenjofe@gmail.com)
  4. Distributed under the Boost Software License,
  5. Version 1.0. (See accompanying file LICENSE_1_0.txt
  6. or copy at http://www.boost.org/LICENSE_1_0.txt).
  7. ]
  8. [section:detected_or detected_or]
  9. template<class Default, template<class...> class Op, class... Args>
  10. using detected_or = __below;
  11. template<class Default, template<class...> class Op, class... Args>
  12. using detected_or_t = typename detected_or<Default, Op, Args...>::type;
  13. __alias An unspecified type with two public member type definitions:
  14. * `value_t` is __true_type if `Op<Args...>` is a valid template-id, otherwise
  15. __false_type
  16. * `type` is `Op<Args...>` if it is a valid template-id, otherwise `Default`
  17. __std_paper [@http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4502.pdf N4502]
  18. __compat Requires C++11 variadic templates and C++11 template aliases.
  19. __header `#include <boost/type_traits/detected_or.hpp>`
  20. __examples
  21. Suppose we wish to declare a type that represents the difference between two values of type T, it should be
  22. T::difference_type if such a type exists, or std::ptrdiff_t otherwise:
  23. template<class T>
  24. using difference_t = typename T::difference_type;
  25. template<class T>
  26. using difference_type = boost::detected_or_t<std::ptrdiff_t, difference_t, T>;
  27. Now the type `difference_type<T>` gives us what we need.
  28. See also: __is_detected, __is_detected_convertible, __is_detected_exact.
  29. [endsect]