Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:09

0001 //===- STLForwardCompat.h - Library features from future STLs ------C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 ///
0009 /// \file
0010 /// This file contains library features backported from future STL versions.
0011 ///
0012 /// These should be replaced with their STL counterparts as the C++ version LLVM
0013 /// is compiled with is updated.
0014 ///
0015 //===----------------------------------------------------------------------===//
0016 
0017 #ifndef LLVM_ADT_STLFORWARDCOMPAT_H
0018 #define LLVM_ADT_STLFORWARDCOMPAT_H
0019 
0020 #include <optional>
0021 #include <type_traits>
0022 
0023 namespace llvm {
0024 
0025 //===----------------------------------------------------------------------===//
0026 //     Features from C++20
0027 //===----------------------------------------------------------------------===//
0028 
0029 template <typename T>
0030 struct remove_cvref // NOLINT(readability-identifier-naming)
0031 {
0032   using type = std::remove_cv_t<std::remove_reference_t<T>>;
0033 };
0034 
0035 template <typename T>
0036 using remove_cvref_t // NOLINT(readability-identifier-naming)
0037     = typename llvm::remove_cvref<T>::type;
0038 
0039 //===----------------------------------------------------------------------===//
0040 //     Features from C++23
0041 //===----------------------------------------------------------------------===//
0042 
0043 // TODO: Remove this in favor of std::optional<T>::transform once we switch to
0044 // C++23.
0045 template <typename T, typename Function>
0046 auto transformOptional(const std::optional<T> &O, const Function &F)
0047     -> std::optional<decltype(F(*O))> {
0048   if (O)
0049     return F(*O);
0050   return std::nullopt;
0051 }
0052 
0053 // TODO: Remove this in favor of std::optional<T>::transform once we switch to
0054 // C++23.
0055 template <typename T, typename Function>
0056 auto transformOptional(std::optional<T> &&O, const Function &F)
0057     -> std::optional<decltype(F(*std::move(O)))> {
0058   if (O)
0059     return F(*std::move(O));
0060   return std::nullopt;
0061 }
0062 
0063 /// Returns underlying integer value of an enum. Backport of C++23
0064 /// std::to_underlying.
0065 template <typename Enum>
0066 [[nodiscard]] constexpr std::underlying_type_t<Enum> to_underlying(Enum E) {
0067   return static_cast<std::underlying_type_t<Enum>>(E);
0068 }
0069 
0070 } // namespace llvm
0071 
0072 #endif // LLVM_ADT_STLFORWARDCOMPAT_H