Dune Core Modules (unstable)

type_traits.hh
1 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi: set et ts=4 sw=2 sts=2:
3 // SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root
4 // SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
5 #ifndef DUNE_COMMON_STD_TYPE_TRAITS_HH
6 #define DUNE_COMMON_STD_TYPE_TRAITS_HH
7 
8 #include <type_traits>
11 
12 #if DUNE_HAVE_CXX_EXPERIMENTAL_IS_DETECTED
13 #include <experimental/type_traits>
14 #endif
15 
16 namespace Dune
17 {
18 
20 
29 namespace Std
30 {
31 
34  using std::bool_constant;
35 
36 #if DUNE_HAVE_CXX_EXPERIMENTAL_IS_DETECTED
37 
38  using std::experimental::nonesuch;
42  using std::experimental::is_detected_v;
45  using std::experimental::is_detected_exact_v;
47  using std::experimental::is_detected_convertible_v;
48 
49 #else // DUNE_HAVE_CXX_EXPERIMENTAL_IS_DETECTED
50 
51  // fallback version of std::experimental::is_detected et al., heavily scribbled
52  // from cppreference.com (but there is actually not much implementation to the thing)
53 
54 #ifndef DOXYGEN
55 
56  namespace Impl {
57 
58  // default version of detector, this gets matched on failure
59  template<typename Default, typename Void, template<typename...> class Op, typename... Args>
60  struct detector
61  {
62  using value_t = std::false_type;
63  using type = Default;
64  };
65 
66  // specialization of detector that matches if Op<Args...> can be instantiated
67  template<typename Default, template<typename...> class Op, typename... Args>
68  struct detector<Default, std::void_t<Op<Args...>>, Op, Args...>
69  {
70  using value_t = std::true_type;
71  using type = Op<Args...>;
72  };
73 
74  }
75 
76 #endif // DOXYGEN
77 
79 
87  struct nonesuch
88  {
89  nonesuch() = delete;
90  ~nonesuch() = delete;
91  nonesuch(const nonesuch&) = delete;
92  void operator=(const nonesuch&) = delete;
93  };
94 
96 
130  template<typename Default, template<typename...> class Op, typename... Args>
131  using detected_or = Impl::detector<Default,void,Op,Args...>;
132 
134 
143  template<template<typename...> class Op, typename... Args>
144  using is_detected = typename detected_or<nonesuch,Op,Args...>::value_t;
145 
146 #ifdef __cpp_variable_templates
148 
157  template<template<typename...> class Op, typename... Args>
158  constexpr bool is_detected_v = is_detected<Op,Args...>::value;
159 #endif // __cpp_variable_templates
160 
162 
172  template<template<typename...> class Op, typename... Args>
173  using detected_t = typename detected_or<nonesuch,Op,Args...>::type;
174 
175 
177 
187  template<typename Default, template<typename...> class Op, typename... Args>
188  using detected_or_t = typename detected_or<Default,Op,Args...>::type;
189 
191 
197  template<typename Expected, template<typename...> class Op, typename... Args>
198  using is_detected_exact = std::is_same<Expected,detected_t<Op,Args...>>;
199 
200 #ifdef __cpp_variable_templates
202 
208  template<typename Expected, template<typename...> class Op, typename... Args>
209  constexpr bool is_detected_exact_v = is_detected_exact<Expected,Op,Args...>::value;
210 #endif // __cpp_variable_templates
211 
213 
219  template<typename Target, template<typename...> class Op, typename... Args>
220  using is_detected_convertible = std::is_convertible<Target,detected_t<Op,Args...>>;
221 
222 #ifdef __cpp_variable_templates
224 
230  template<typename Target, template<typename...> class Op, typename... Args>
231  constexpr bool is_detected_convertible_v = is_detected_convertible<Target,Op,Args...>::value;
232 #endif // __cpp_variable_templates
233 
234 #endif // DUNE_HAVE_CXX_EXPERIMENTAL_IS_DETECTED
235 
236 } // namespace Std
237 
238 
239 namespace detail
240 {
241  template <class Type>
242  [[deprecated("Type extraction of `TargetType` has failed. Inspect the code calling `detected_or_fallback_t` for getting the source of this warning!")]]
243  Type warningIfNotDefined(const Std::nonesuch*);
244 
245  template <class Type, class T>
246  Type warningIfNotDefined(const T*);
247 }
248 
250 template <template<typename...> class Fallback,
251  template<typename...> class TargetType, typename... Args>
253  detail::warningIfNotDefined<Std::detected_t<Fallback, Args...> >(std::declval<const Std::detected_t<TargetType, Args...>*>())),
254  TargetType, Args...>;
255 
256 
257 } // namespace Dune
258 
259 #endif // #ifndef DUNE_COMMON_STD_TYPE_TRAITS_HH
typename detected_or< Default, Op, Args... >::type detected_or_t
Returns Op<Args...> if that is valid; otherwise returns the fallback type Default.
Definition: type_traits.hh:188
std::is_same< Expected, detected_t< Op, Args... > > is_detected_exact
Checks whether Op<Args...> is Expected without causing an error if Op<Args...> is invalid.
Definition: type_traits.hh:198
typename detected_or< nonesuch, Op, Args... >::type detected_t
Returns Op<Args...> if that is valid; otherwise returns nonesuch.
Definition: type_traits.hh:173
typename Impl::voider< Types... >::type void_t
Is void for all valid input types. The workhorse for C++11 SFINAE-techniques.
Definition: typetraits.hh:40
typename detected_or< nonesuch, Op, Args... >::value_t is_detected
Detects whether Op<Args...> is valid.
Definition: type_traits.hh:144
Impl::detector< Default, void, Op, Args... > detected_or
Detects whether Op<Args...> is valid and makes the result available.
Definition: type_traits.hh:131
std::is_convertible< Target, detected_t< Op, Args... > > is_detected_convertible
Checks whether Op<Args...> is convertible to Target without causing an error if Op<Args....
Definition: type_traits.hh:220
Dune namespace.
Definition: alignedallocator.hh:13
Std::detected_or_t< decltype(detail::warningIfNotDefined< Std::detected_t< Fallback, Args... > >(std::declval< const Std::detected_t< TargetType, Args... > * >())), TargetType, Args... > detected_or_fallback_t
This type will be either TargetType<Args...> if it exists, or the Fallback<Args......
Definition: type_traits.hh:254
Type representing a lookup failure by std::detected_or and friends.
Definition: type_traits.hh:88
Traits for type conversions and type information.
Utilities for type computations, constraining overloads, ...
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.80.0 (May 4, 22:30, 2024)