1 //===-- sanitizer_type_traits.h ---------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // Implements a subset of C++ type traits. This is so we can avoid depending
10 // on system C++ headers.
12 //===----------------------------------------------------------------------===//
13 #ifndef SANITIZER_TYPE_TRAITS_H
14 #define SANITIZER_TYPE_TRAITS_H
16 #include "sanitizer_common/sanitizer_internal_defs.h"
18 namespace __sanitizer
{
21 static const bool value
= true;
25 static const bool value
= false;
30 // Type trait to compare if types are the same.
34 // is_same<int,int>::value - True
35 // is_same<int,char>::value - False
37 template <typename T
, typename U
>
38 struct is_same
: public false_type
{};
41 struct is_same
<T
, T
> : public true_type
{};
43 // conditional<B, T, F>
45 // Defines type as T if B is true or as F otherwise.
46 // E.g. the following is true
49 // is_same<int, conditional<true, int, double>::type>::value
50 // is_same<double, conditional<false, int, double>::type>::value
52 template <bool B
, class T
, class F
>
57 template <class T
, class F
>
58 struct conditional
<false, T
, F
> {
63 struct remove_reference
{
67 struct remove_reference
<T
&> {
71 struct remove_reference
<T
&&> {
76 WARN_UNUSED_RESULT
inline typename remove_reference
<T
>::type
&& move(T
&& t
) {
77 return static_cast<typename remove_reference
<T
>::type
&&>(t
);
81 WARN_UNUSED_RESULT
inline constexpr T
&& forward(
82 typename remove_reference
<T
>::type
& t
) {
83 return static_cast<T
&&>(t
);
87 WARN_UNUSED_RESULT
inline constexpr T
&& forward(
88 typename remove_reference
<T
>::type
&& t
) {
89 return static_cast<T
&&>(t
);
92 template <class T
, T v
>
93 struct integral_constant
{
94 static constexpr const T value
= v
;
96 typedef integral_constant type
;
97 constexpr operator value_type() const { return value
; }
98 constexpr value_type
operator()() const { return value
; }
101 #ifndef __has_builtin
102 # define __has_builtin(x) 0
105 #if __has_builtin(__is_trivially_destructible)
108 struct is_trivially_destructible
109 : public integral_constant
<bool, __is_trivially_destructible(T
)> {};
111 #elif __has_builtin(__has_trivial_destructor)
114 struct is_trivially_destructible
115 : public integral_constant
<bool, __has_trivial_destructor(T
)> {};
120 struct is_trivially_destructible
121 : public integral_constant
<bool, /* less efficient fallback */ false> {};
125 #if __has_builtin(__is_trivially_copyable)
128 struct is_trivially_copyable
129 : public integral_constant
<bool, __is_trivially_copyable(T
)> {};
134 struct is_trivially_copyable
135 : public integral_constant
<bool, /* less efficient fallback */ false> {};
139 } // namespace __sanitizer