summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Peter Kasting <pkasting@chromium.org> 2022-10-13 17:57:39 +0000
committer Treehugger Robot <treehugger-gerrit@google.com> 2022-10-14 09:45:42 +0000
commit5c769533d04343895e1eae83fee5eb1d1d8db2a1 (patch)
treed9b26835e89974ab39bd397f92119634a6683ce4
parentb1795307adc831b0286a10e9e3155bc704eb9486 (diff)
Fix compile in C++20 mode.
* std::result_of is deprecated; switch to std::invoke_result * Add use of std::string_view::(starts,ends)_with as appropriate (see https://issuetracker.google.com/issues/253467211 ) Test: Compiles with -std=c++20 Signed-off-by: Peter Kasting <pkasting@chromium.org> Change-Id: I8b6bf79b3fb663e2151145cc1eec260fd3e2e2b4
-rw-r--r--libartbase/base/safe_map.h2
-rw-r--r--libartbase/base/string_view_cpp20.h15
2 files changed, 11 insertions, 6 deletions
diff --git a/libartbase/base/safe_map.h b/libartbase/base/safe_map.h
index 7ae85d45dc..c6d4353038 100644
--- a/libartbase/base/safe_map.h
+++ b/libartbase/base/safe_map.h
@@ -149,7 +149,7 @@ class SafeMap {
template <typename CreateFn>
V& GetOrCreate(const K& k, CreateFn create) {
- static_assert(std::is_same_v<V, std::result_of_t<CreateFn()>>,
+ static_assert(std::is_same_v<V, std::invoke_result_t<CreateFn>>,
"Argument `create` should return a value of type V.");
auto lb = lower_bound(k);
if (lb != end() && !key_comp()(k, lb->first)) {
diff --git a/libartbase/base/string_view_cpp20.h b/libartbase/base/string_view_cpp20.h
index 2c11a2f2b8..9bd29d5c52 100644
--- a/libartbase/base/string_view_cpp20.h
+++ b/libartbase/base/string_view_cpp20.h
@@ -21,18 +21,23 @@
namespace art {
-// Replacement functions for std::string_view::starts_with(), ends_with()
-// which shall be available in C++20.
-#if __cplusplus >= 202000L
-#error "When upgrading to C++20, remove this error and file a bug to remove this workaround."
-#endif
+// When this code is only compiled on C++20+, these wrappers can be removed and
+// calling code changed to call the string_view methods directly.
inline bool StartsWith(std::string_view sv, std::string_view prefix) {
+#if !defined(__cpp_lib_starts_ends_with) || __cpp_lib_starts_ends_with < 201711L
return sv.substr(0u, prefix.size()) == prefix;
+#else
+ return sv.starts_with(prefix);
+#endif
}
inline bool EndsWith(std::string_view sv, std::string_view suffix) {
+#if !defined(__cpp_lib_starts_ends_with) || __cpp_lib_starts_ends_with < 201711L
return sv.size() >= suffix.size() && sv.substr(sv.size() - suffix.size()) == suffix;
+#else
+ return sv.ends_with(suffix);
+#endif
}
} // namespace art