STLForwardCompat.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- STLForwardCompat.h - Library features from future STLs ------C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. ///
  14. /// \file
  15. /// This file contains library features backported from future STL versions.
  16. ///
  17. /// These should be replaced with their STL counterparts as the C++ version LLVM
  18. /// is compiled with is updated.
  19. ///
  20. //===----------------------------------------------------------------------===//
  21. #ifndef LLVM_ADT_STLFORWARDCOMPAT_H
  22. #define LLVM_ADT_STLFORWARDCOMPAT_H
  23. #include <optional>
  24. #include <type_traits>
  25. namespace llvm {
  26. //===----------------------------------------------------------------------===//
  27. // Features from C++20
  28. //===----------------------------------------------------------------------===//
  29. template <typename T>
  30. struct remove_cvref // NOLINT(readability-identifier-naming)
  31. {
  32. using type = std::remove_cv_t<std::remove_reference_t<T>>;
  33. };
  34. template <typename T>
  35. using remove_cvref_t // NOLINT(readability-identifier-naming)
  36. = typename llvm::remove_cvref<T>::type;
  37. //===----------------------------------------------------------------------===//
  38. // Features from C++23
  39. //===----------------------------------------------------------------------===//
  40. // TODO: Remove this in favor of std::optional<T>::transform once we switch to
  41. // C++23.
  42. template <typename T, typename Function>
  43. auto transformOptional(const std::optional<T> &O, const Function &F)
  44. -> std::optional<decltype(F(*O))> {
  45. if (O)
  46. return F(*O);
  47. return std::nullopt;
  48. }
  49. // TODO: Remove this in favor of std::optional<T>::transform once we switch to
  50. // C++23.
  51. template <typename T, typename Function>
  52. auto transformOptional(std::optional<T> &&O, const Function &F)
  53. -> std::optional<decltype(F(*std::move(O)))> {
  54. if (O)
  55. return F(*std::move(O));
  56. return std::nullopt;
  57. }
  58. } // namespace llvm
  59. #endif // LLVM_ADT_STLFORWARDCOMPAT_H
  60. #ifdef __GNUC__
  61. #pragma GCC diagnostic pop
  62. #endif