Sane C++ Libraries
C++ Platform Abstraction Libraries
TypeTraits.h
1// Copyright (c) Stefano Cristiano
2// SPDX-License-Identifier: MIT
3#pragma once
4#include "../Foundation/PrimitiveTypes.h"
10
14
15namespace SC
16{
18namespace TypeTraits
19{
20// clang-format off
23
25template <bool B, class T = void> struct EnableIf {};
26template <class T> struct EnableIf<true, T> { using type = T; };
27
29template <typename T, typename U> struct IsSame { static constexpr bool value = false; };
30template <typename T> struct IsSame<T, T> { static constexpr bool value = true; };
31
33template <class T> struct RemovePointer { using type = T; };
34template <class T> struct RemovePointer<T*> { using type = T; };
35template <class T> struct RemovePointer<T**> { using type = T; };
36
38template <class T> struct AddReference { using type = T&; };
39template <class T> struct AddReference<T&> { using type = T ; };
40template <class T> struct AddReference<T&&> { using type = T ; };
41
43template <class T> struct AddPointer { using type = typename RemoveReference<T>::type*; };
44
46template <class T> struct RemoveConst { using type = T;};
47template <class T> struct RemoveConst<const T> { using type = T; };
48
50template <typename R> struct ReturnType;
51template <typename R, typename... Args> struct ReturnType<R(Args...)> { using type = R; };
52template <typename R, typename... Args> struct ReturnType<R(*)(Args...)> { using type = R; };
53template <typename R, typename C, typename... Args> struct ReturnType<R(C::*)(Args...)> { using type = R; };
54
56template <typename T> struct IsConst { using type = T; static constexpr bool value = false; };
57template <typename T> struct IsConst<const T> { using type = T; static constexpr bool value = true; };
58
60template <typename T> struct IsTriviallyCopyable { static constexpr bool value = __is_trivially_copyable(T); };
61
63template <class T> struct IsReference { static constexpr bool value = IsLValueReference<T>::value || IsRValueReference<T>::value; };
64
66template <bool B, class T, class F> struct Conditional { using type = T; };
67template <class T, class F> struct Conditional<false, T, F> { using type = F; };
68
70template <bool B, class T, class F> using ConditionalT = typename Conditional<B,T,F>::type;
71
73template <typename U, typename T> struct SameConstnessAs { using type = typename Conditional<IsConst<U>::value, const T, T>::type; };
74
76template <typename T, size_t N> constexpr auto SizeOfArray(const T (&)[N]) { return N; }
77
79// clang-format on
80} // namespace TypeTraits
81} // namespace SC
82
83// Defining a constexpr destructor is C++ 20+
84#if SC_LANGUAGE_CPP_AT_LEAST_20
85#define SC_LANGUAGE_CONSTEXPR_DESTRUCTOR constexpr
86#else
87#define SC_LANGUAGE_CONSTEXPR_DESTRUCTOR
88#endif
typename Conditional< B, T, F >::type ConditionalT
ConditionalT is an alias template that resolves to type T if a boolean value is true,...
Definition: TypeTraits.h:70
constexpr auto SizeOfArray(const T(&)[N])
SizeOfArray is a constexpr function that returns the compile-time size N of a plain C array.
Definition: TypeTraits.h:76
AddPointer adds a pointer qualification to a type T if it is not already a pointer.
Definition: TypeTraits.h:43
AddReference adds a reference qualifier to a type T if it is not already a reference.
Definition: TypeTraits.h:38
Conditional defines a type to be T if a boolean value is true, F otherwise.
Definition: TypeTraits.h:66
EnableIf conditionally defines a type if a boolean template parameter is true.
Definition: TypeTraits.h:25
IsConst evaluates to true if the provided type T is const, false otherwise.
Definition: TypeTraits.h:56
Determines if a type is an lvalue reference.
Definition: Compiler.h:252
Determines if a type is an rvalue reference.
Definition: Compiler.h:255
IsReference evaluates to true if the type T is a reference, false otherwise.
Definition: TypeTraits.h:63
IsSame evaluates to true if the provided types T and U are the same, false otherwise.
Definition: TypeTraits.h:29
IsTriviallyCopyable evaluates to true if the type T can be trivially copied, false otherwise.
Definition: TypeTraits.h:60
RemoveConst removes the const qualification from a type T.
Definition: TypeTraits.h:46
RemovePointer removes the pointer qualification from a type T.
Definition: TypeTraits.h:33
ReturnType extracts the return type from different forms of function types.
Definition: TypeTraits.h:50
SameConstnessAs modifies type T to have the const-qualification of U.
Definition: TypeTraits.h:73