LLVM 22.0.0git
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 LLVM_SUPPORT_TYPE_TRAITS_H
14#define LLVM_SUPPORT_TYPE_TRAITS_H
15
17#include <type_traits>
18#include <utility>
19
20namespace llvm {
21
22/// Metafunction that determines whether the given type is either an
23/// integral type or an enumeration type, including enum classes.
24///
25/// Note that this accepts potentially more integral types than is_integral
26/// because it is based on being implicitly convertible to an integral type.
27/// Also note that enum classes aren't implicitly convertible to integral types,
28/// the value may therefore need to be explicitly converted before being used.
29template <typename T> class is_integral_or_enum {
30 using UnderlyingT = std::remove_reference_t<T>;
31
32public:
33 static const bool value =
34 !std::is_class_v<UnderlyingT> && // Filter conversion operators.
35 !std::is_pointer_v<UnderlyingT> &&
36 !std::is_floating_point_v<UnderlyingT> &&
37 (std::is_enum_v<UnderlyingT> ||
38 std::is_convertible_v<UnderlyingT, unsigned long long>);
39};
40
41/// If T is a pointer, just return it. If it is not, return T&.
42template <typename T> struct add_lvalue_reference_if_not_pointer {
43 using type = std::conditional_t<std::is_pointer_v<T>, T, T &>;
44};
45
46/// If T is a pointer to X, return a pointer to const X. If it is not,
47/// return const T.
48template <typename T> struct add_const_past_pointer {
49 using type = std::conditional_t<std::is_pointer_v<T>,
50 const std::remove_pointer_t<T> *, const T>;
51};
52
53template <typename T> struct const_pointer_or_const_ref {
54 using type =
55 std::conditional_t<std::is_pointer_v<T>,
56 typename add_const_past_pointer<T>::type, const T &>;
57};
58
59} // namespace llvm
60
61#endif // LLVM_SUPPORT_TYPE_TRAITS_H
#define T
Metafunction that determines whether the given type is either an integral type or an enumeration type...
Definition type_traits.h:29
static const bool value
Definition type_traits.h:33
This is an optimization pass for GlobalISel generic memory operations.
If T is a pointer to X, return a pointer to const X.
Definition type_traits.h:48
std::conditional_t< std::is_pointer_v< T >, const std::remove_pointer_t< T > *, const T > type
Definition type_traits.h:49
If T is a pointer, just return it. If it is not, return T&.
Definition type_traits.h:42
std::conditional_t< std::is_pointer_v< T >, T, T & > type
Definition type_traits.h:43
std::conditional_t< std::is_pointer_v< T >, typename add_const_past_pointer< T >::type, const T & > type
Definition type_traits.h:54