[libcxx-commits] [libcxx] [libc++][strings] P2591R5: Concatenation of strings and string views (PR #88389)
Hristo Hristov via libcxx-commits
libcxx-commits at lists.llvm.org
Sun Jul 14 07:24:44 PDT 2024
================
@@ -0,0 +1,215 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20, c++23
+
+// <string>
+
+// [string.op.plus]
+//
+// template<class charT, class traits, class Allocator>
+// constexpr basic_string<charT, traits, Allocator>
+// operator+(const basic_string<charT, traits, Allocator>& lhs,
+// type_identity_t<basic_string_view<charT, traits>> rhs); // Since C++26
+// template<class charT, class traits, class Allocator>
+// constexpr basic_string<charT, traits, Allocator>
+// operator+(basic_string<charT, traits, Allocator>&& lhs,
+// type_identity_t<basic_string_view<charT, traits>> rhs); // Since C++26
+// template<class charT, class traits, class Allocator>
+// constexpr basic_string<charT, traits, Allocator>
+// operator+(type_identity_t<basic_string_view<charT, traits>> lhs,
+// const basic_string<charT, traits, Allocator>& rhs); // Since C++26
+// template<class charT, class traits, class Allocator>
+// constexpr basic_string<charT, traits, Allocator>
+// operator+(type_identity_t<basic_string_view<charT, traits>> lhs,
+// basic_string<charT, traits, Allocator>&& rhs); // Since C++26
+
+#include <cassert>
+#include <concepts>
+#include <string>
+#include <utility>
+
+#include "asan_testing.h"
+#include "constexpr_char_traits.h"
+#include "make_string.h"
+#include "min_allocator.h"
+#include "test_allocator.h"
+#include "test_macros.h"
+
+template <typename CharT, class TraitsT = std::char_traits<CharT>>
+class ConvertibleToStringView {
+public:
+ constexpr explicit ConvertibleToStringView(const CharT* cs) : cs_{cs} {}
+
+ constexpr operator std::basic_string_view<CharT, TraitsT>() { return std::basic_string_view<CharT, TraitsT>(cs_); }
+ constexpr operator std::basic_string_view<CharT, TraitsT>() const {
+ return std::basic_string_view<CharT, TraitsT>(cs_);
+ }
+
+private:
+ const CharT* cs_;
+};
+
+static_assert(std::constructible_from<std::basic_string_view<char>, const ConvertibleToStringView<char>>);
+static_assert(std::convertible_to<const ConvertibleToStringView<char>, std::basic_string_view<char>>);
+
+static_assert(std::constructible_from<std::basic_string_view<char>, ConvertibleToStringView<char>>);
+static_assert(std::convertible_to<ConvertibleToStringView<char>, std::basic_string_view<char>>);
+
+#define CS(S) MAKE_CSTRING(CharT, S)
+
+template <template <typename, typename> typename StringViewT, typename CharT, typename TraitsT, typename AllocT>
+constexpr void test(const CharT* x, const CharT* y, const CharT* expected) {
+ AllocT allocator;
+
+ // string& + string_view
+ {
+ std::basic_string<CharT, TraitsT, AllocT> st{x, allocator};
+ StringViewT<CharT, TraitsT> sv{y};
+
+ std::same_as<std::basic_string<CharT, TraitsT, AllocT>> decltype(auto) result = st + sv;
+ assert(result == expected);
+ assert(result.get_allocator() == allocator);
+ LIBCPP_ASSERT(is_string_asan_correct(st + sv));
+ }
+ // const string& + string_view
+ {
+ const std::basic_string<CharT, TraitsT, AllocT> st{x, allocator};
+ StringViewT<CharT, TraitsT> sv{y};
+
+ std::same_as<std::basic_string<CharT, TraitsT, AllocT>> decltype(auto) result = st + sv;
+ assert(result == expected);
+ assert(result.get_allocator() == allocator);
+ LIBCPP_ASSERT(is_string_asan_correct(st + sv));
+ }
+ // string&& + string_view
+ {
+ std::basic_string<CharT, TraitsT, AllocT> st{x, allocator};
+ StringViewT<CharT, TraitsT> sv{y};
+
+ std::same_as<std::basic_string<CharT, TraitsT, AllocT>> decltype(auto) result = std::move(st) + sv;
+ assert(result == expected);
+ assert(result.get_allocator() == allocator);
+ LIBCPP_ASSERT(is_string_asan_correct(std::move(st) + sv));
+ }
+ // string_view + string&
+ {
+ StringViewT<CharT, TraitsT> sv{x};
+ std::basic_string<CharT, TraitsT, AllocT> st{y, allocator};
+
+ std::same_as<std::basic_string<CharT, TraitsT, AllocT>> decltype(auto) result = sv + st;
+ assert(result == expected);
+ assert(result.get_allocator() == allocator);
+ LIBCPP_ASSERT(is_string_asan_correct(sv + st));
+ }
+ // string_view + const string&
+ {
+ StringViewT<CharT, TraitsT> sv{x};
+ const std::basic_string<CharT, TraitsT, AllocT> st{y, allocator};
+
+ std::same_as<std::basic_string<CharT, TraitsT, AllocT>> decltype(auto) result = sv + st;
+ assert(result == expected);
+ assert(result.get_allocator() == allocator);
+ LIBCPP_ASSERT(is_string_asan_correct(sv + st));
+ }
+ // string_view + string&&
+ {
+ // TODO: Create a `basic_string` to workaround clang bug:
+ // https://github.com/llvm/llvm-project/issues/92382
+ // Comparison between pointers to a string literal and some other object results in constant evaluation failure.
+ if constexpr (std::same_as<StringViewT<CharT, TraitsT>, std::basic_string_view<CharT, TraitsT>>) {
----------------
H-G-Hristov wrote:
FYI: I decided to not enable this test for the `convertible_to_string_view` case due to the Clang bug. Once it is resolved, the test should be enabled/fixed.
https://github.com/llvm/llvm-project/pull/88389
More information about the libcxx-commits
mailing list