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