WPILibC++ 2024.1.1-beta-4
type_traits.h
Go to the documentation of this file.
1//===- llvm/Support/type_traits.h - Simplfied type traits -------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file provides useful additions to the standard type_traits library.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef WPIUTIL_WPI_TYPE_TRAITS_H
14#define WPIUTIL_WPI_TYPE_TRAITS_H
15
16#include "wpi/Compiler.h"
17#include <type_traits>
18#include <utility>
19
20namespace wpi {
21
22
23/// Metafunction that determines whether the given type is either an
24/// integral type or an enumeration type, including enum classes.
25///
26/// Note that this accepts potentially more integral types than is_integral
27/// because it is based on being implicitly convertible to an integral type.
28/// Also note that enum classes aren't implicitly convertible to integral types,
29/// the value may therefore need to be explicitly converted before being used.
30template <typename T> class is_integral_or_enum {
31 using UnderlyingT = std::remove_reference_t<T>;
32
33public:
34 static const bool value =
35 !std::is_class_v<UnderlyingT> && // Filter conversion operators.
36 !std::is_pointer_v<UnderlyingT> &&
37 !std::is_floating_point_v<UnderlyingT> &&
38 (std::is_enum_v<UnderlyingT> ||
39 std::is_convertible_v<UnderlyingT, unsigned long long>);
40};
41
42/// If T is a pointer, just return it. If it is not, return T&.
43template<typename T, typename Enable = void>
45
46template <typename T>
48 T, std::enable_if_t<std::is_pointer_v<T>>> {
49 using type = T;
50};
51
52/// If T is a pointer to X, return a pointer to const X. If it is not,
53/// return const T.
54template<typename T, typename Enable = void>
55struct add_const_past_pointer { using type = const T; };
56
57template <typename T>
58struct add_const_past_pointer<T, std::enable_if_t<std::is_pointer_v<T>>> {
59 using type = const std::remove_pointer_t<T> *;
60};
61
62template <typename T, typename Enable = void>
64 using type = const T &;
65};
66template <typename T>
67struct const_pointer_or_const_ref<T, std::enable_if_t<std::is_pointer_v<T>>> {
69};
70
71namespace detail {
72template<class T>
74 T t;
75};
76
77} // end namespace detail
78
79// https://stackoverflow.com/questions/55288555/c-check-if-statement-can-be-evaluated-constexpr
80template<class Lambda, int=(Lambda{}(), 0)>
81constexpr bool is_constexpr(Lambda) { return true; }
82constexpr bool is_constexpr(...) { return false; }
83
84} // end namespace wpi
85
86#endif // WPIUTIL_WPI_TYPE_TRAITS_H
Metafunction that determines whether the given type is either an integral type or an enumeration type...
Definition: type_traits.h:30
static const bool value
Definition: type_traits.h:34
typename std::enable_if< B, T >::type enable_if_t
Definition: core.h:256
detail namespace with internal helper functions
Definition: ranges.h:23
Definition: array.h:89
Definition: ntcore_cpp.h:26
constexpr bool is_constexpr(Lambda)
Definition: type_traits.h:81
const std::remove_pointer_t< T > * type
Definition: type_traits.h:59
If T is a pointer to X, return a pointer to const X.
Definition: type_traits.h:55
const T type
Definition: type_traits.h:55
If T is a pointer, just return it. If it is not, return T&.
Definition: type_traits.h:44
T & type
Definition: type_traits.h:44
typename add_const_past_pointer< T >::type type
Definition: type_traits.h:68
Definition: type_traits.h:63
const T & type
Definition: type_traits.h:64
Definition: type_traits.h:73
T t
Definition: type_traits.h:74