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:is_detected is_detected] |
| 11 | |
| 12 | template<template<class...> class Op, class... Args> |
| 13 | using is_detected = __below; |
| 14 | |
| 15 | template<template<class...> class Op, class... Args> |
| 16 | constexpr bool is_detected_v = is_detected<Op, Args...>::value; |
| 17 | |
| 18 | __alias If `Op<Args...>` is a valid template-id, aliases __true_type, |
| 19 | otherwise aliases __false_type. |
| 20 | |
| 21 | __std_paper [@http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4502.pdf N4502] |
| 22 | |
| 23 | __compat Requires C++11 variadic templates and C++11 template aliases. |
| 24 | |
| 25 | __header `#include <boost/type_traits/is_detected.hpp>` |
| 26 | |
| 27 | __examples |
| 28 | |
| 29 | Suppose we wish to "reset" a value of type T, if the type has a `clear()` member function then we should call |
| 30 | it, otherwise we should assign a default constructed value: |
| 31 | |
| 32 | template<class T> |
| 33 | using clear_t = decltype(boost::declval<T&>().clear()); |
| 34 | |
| 35 | template<class T> |
| 36 | void clear(T& value) |
| 37 | { |
| 38 | if constexpr (boost::is_detected_v<clear_t, T>) { |
| 39 | value.clear(); |
| 40 | } else { |
| 41 | value = T(); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | See also: __is_detected_convertible, __is_detected_exact. |
| 46 | |
| 47 | [endsect] |