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