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>
9#include <dune-common-config.hh> // DUNE_HAVE_CXX_EXPERIMENTAL_IS_DETECTED
12
13#if DUNE_HAVE_CXX_EXPERIMENTAL_IS_DETECTED
14#include <experimental/type_traits>
15#endif
16
17namespace Dune
18{
19
21
30namespace Std
31{
32
35 using std::bool_constant;
36
37#if DUNE_HAVE_CXX_EXPERIMENTAL_IS_DETECTED
38
39 using std::experimental::nonesuch;
43 using std::experimental::is_detected_v;
46 using std::experimental::is_detected_exact_v;
48 using std::experimental::is_detected_convertible_v;
49
50#else // DUNE_HAVE_CXX_EXPERIMENTAL_IS_DETECTED
51
52 // fallback version of std::experimental::is_detected et al., heavily scribbled
53 // from cppreference.com (but there is actually not much implementation to the thing)
54
55#ifndef DOXYGEN
56
57 namespace Impl {
58
59 // default version of detector, this gets matched on failure
60 template<typename Default, typename Void, template<typename...> class Op, typename... Args>
61 struct detector
62 {
63 using value_t = std::false_type;
64 using type = Default;
65 };
66
67 // specialization of detector that matches if Op<Args...> can be instantiated
68 template<typename Default, template<typename...> class Op, typename... Args>
69 struct detector<Default, std::void_t<Op<Args...>>, Op, Args...>
70 {
71 using value_t = std::true_type;
72 using type = Op<Args...>;
73 };
74
75 }
76
77#endif // DOXYGEN
78
80
88 struct nonesuch
89 {
90 nonesuch() = delete;
91 ~nonesuch() = delete;
92 nonesuch(const nonesuch&) = delete;
93 void operator=(const nonesuch&) = delete;
94 };
95
97
131 template<typename Default, template<typename...> class Op, typename... Args>
132 using detected_or = Impl::detector<Default,void,Op,Args...>;
133
135
144 template<template<typename...> class Op, typename... Args>
145 using is_detected = typename detected_or<nonesuch,Op,Args...>::value_t;
146
147#ifdef __cpp_variable_templates
149
158 template<template<typename...> class Op, typename... Args>
159 constexpr bool is_detected_v = is_detected<Op,Args...>::value;
160#endif // __cpp_variable_templates
161
163
173 template<template<typename...> class Op, typename... Args>
174 using detected_t = typename detected_or<nonesuch,Op,Args...>::type;
175
176
178
188 template<typename Default, template<typename...> class Op, typename... Args>
189 using detected_or_t = typename detected_or<Default,Op,Args...>::type;
190
192
198 template<typename Expected, template<typename...> class Op, typename... Args>
199 using is_detected_exact = std::is_same<Expected,detected_t<Op,Args...>>;
200
201#ifdef __cpp_variable_templates
203
209 template<typename Expected, template<typename...> class Op, typename... Args>
210 constexpr bool is_detected_exact_v = is_detected_exact<Expected,Op,Args...>::value;
211#endif // __cpp_variable_templates
212
214
220 template<typename Target, template<typename...> class Op, typename... Args>
221 using is_detected_convertible = std::is_convertible<Target,detected_t<Op,Args...>>;
222
223#ifdef __cpp_variable_templates
225
231 template<typename Target, template<typename...> class Op, typename... Args>
232 constexpr bool is_detected_convertible_v = is_detected_convertible<Target,Op,Args...>::value;
233#endif // __cpp_variable_templates
234
235#endif // DUNE_HAVE_CXX_EXPERIMENTAL_IS_DETECTED
236
237} // namespace Std
238
239
240namespace detail
241{
242 template <class Type>
243 [[deprecated("Type extraction of `TargetType` has failed. Inspect the code calling `detected_or_fallback_t` for getting the source of this warning!")]]
244 Type warningIfNotDefined(const Std::nonesuch*);
245
246 template <class Type, class T>
247 Type warningIfNotDefined(const T*);
248}
249
251template <template<typename...> class Fallback,
252 template<typename...> class TargetType, typename... Args>
254 detail::warningIfNotDefined<Std::detected_t<Fallback, Args...> >(std::declval<const Std::detected_t<TargetType, Args...>*>())),
255 TargetType, Args...>;
256
257
258} // namespace Dune
259
260#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:189
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:199
typename detected_or< nonesuch, Op, Args... >::type detected_t
Returns Op<Args...> if that is valid; otherwise returns nonesuch.
Definition: type_traits.hh:174
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:145
Impl::detector< Default, void, Op, Args... > detected_or
Detects whether Op<Args...> is valid and makes the result available.
Definition: type_traits.hh:132
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:221
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:255
STL namespace.
Type representing a lookup failure by std::detected_or and friends.
Definition: type_traits.hh:89
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.111.3 (Dec 21, 23:30, 2024)