From: Jonathan Wakely <jwakely@redhat.com>
To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org
Subject: [committed] libstdc++: Make std::string_view(Range&&) constructor explicit
Date: Thu, 4 Aug 2022 19:38:54 +0100 [thread overview]
Message-ID: <20220804183854.753948-1-jwakely@redhat.com> (raw)
Tested powerpc64le-linux, pushed to trunk.
-- >8 --
The P2499R0 paper was recently approved for C++23.
libstdc++-v3/ChangeLog:
* include/std/string_view (basic_string_view(Range&&)): Add
explicit as per P2499R0.
* testsuite/21_strings/basic_string_view/cons/char/range_c++20.cc:
Adjust implicit conversions. Check implicit conversions fail.
* testsuite/21_strings/basic_string_view/cons/wchar_t/range_c++20.cc:
Likewise.
---
libstdc++-v3/include/std/string_view | 2 +-
.../cons/char/range_c++20.cc | 28 ++++++++++++++---
.../cons/wchar_t/range_c++20.cc | 30 +++++++++++++++----
3 files changed, 50 insertions(+), 10 deletions(-)
diff --git a/libstdc++-v3/include/std/string_view b/libstdc++-v3/include/std/string_view
index bccf4d1847f..30ff136b1cb 100644
--- a/libstdc++-v3/include/std/string_view
+++ b/libstdc++-v3/include/std/string_view
@@ -162,7 +162,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
})
&& (!requires { typename _DRange::traits_type; }
|| is_same_v<typename _DRange::traits_type, _Traits>)
- constexpr
+ constexpr explicit
basic_string_view(_Range&& __r)
noexcept(noexcept(ranges::size(__r)) && noexcept(ranges::data(__r)))
: _M_len(ranges::size(__r)), _M_str(ranges::data(__r))
diff --git a/libstdc++-v3/testsuite/21_strings/basic_string_view/cons/char/range_c++20.cc b/libstdc++-v3/testsuite/21_strings/basic_string_view/cons/char/range_c++20.cc
index bd50c3058e6..a5745fcb603 100644
--- a/libstdc++-v3/testsuite/21_strings/basic_string_view/cons/char/range_c++20.cc
+++ b/libstdc++-v3/testsuite/21_strings/basic_string_view/cons/char/range_c++20.cc
@@ -36,7 +36,7 @@ test01()
};
R r;
- std::string_view s = r;
+ std::string_view s{r};
VERIFY( s == r.str );
VERIFY( s.data() == std::ranges::data(r) );
VERIFY( s.size() == std::ranges::size(r) );
@@ -50,10 +50,15 @@ test01()
static_assert( std::ranges::contiguous_range<R2> );
static_assert( std::ranges::sized_range<R2> );
R2 r2;
- std::string_view s2 = r2; // uses conversion to string_view
+ std::string_view s2(r2); // uses conversion to string_view
VERIFY( s2 == "Out of range" );
VERIFY( std::string_view(const_cast<const R2&>(r2)) == s2 );
+ // And again using copy-initialization instead of direct-initialization.
+ std::string_view s2_implicit = r2; // uses conversion to string_view
+ VERIFY( s2_implicit == "Out of range" );
+ VERIFY( std::string_view(const_cast<const R2&>(r2)) == s2_implicit );
+
struct R3 : R
{
using R::begin;
@@ -91,7 +96,7 @@ test01()
static_assert( std::ranges::contiguous_range<R5> );
static_assert( std::ranges::sized_range<R5> );
R5 r5;
- std::string_view s5 = r5; // Uses range constructor
+ std::string_view s5(r5); // Uses range constructor
VERIFY( s5 == r5.str );
s5 = std::string_view(std::move(r5)); // In C++20 this used conversion op.
VERIFY( s5 == r5.str ); // In C++23 it uses range constructor.
@@ -156,15 +161,30 @@ test04()
};
R r;
- std::basic_string_view s = r; // Use deduction guide.
+ std::basic_string_view s(r); // Use deduction guide.
static_assert( std::is_same_v<decltype(s), std::string_view> );
}
+void
+test05()
+{
+ struct R
+ {
+ const char* begin() const { return nullptr; }
+ const char* end() const { return nullptr; }
+ };
+
+ // P2499R0 string_view range constructor should be explicit
+ // P2516R0 string_view is implicitly convertible from what?
+ static_assert( ! std::is_convertible_v<R, std::string_view> );
+}
+
int main()
{
test01();
test02();
test03();
test04();
+ test05();
}
diff --git a/libstdc++-v3/testsuite/21_strings/basic_string_view/cons/wchar_t/range_c++20.cc b/libstdc++-v3/testsuite/21_strings/basic_string_view/cons/wchar_t/range_c++20.cc
index 0b28220d862..af3c986e56f 100644
--- a/libstdc++-v3/testsuite/21_strings/basic_string_view/cons/wchar_t/range_c++20.cc
+++ b/libstdc++-v3/testsuite/21_strings/basic_string_view/cons/wchar_t/range_c++20.cc
@@ -36,7 +36,7 @@ test01()
};
R r;
- std::wstring_view s = r;
+ std::wstring_view s{r};
VERIFY( s == r.str );
VERIFY( s.data() == std::ranges::data(r) );
VERIFY( s.size() == std::ranges::size(r) );
@@ -50,10 +50,15 @@ test01()
static_assert( std::ranges::contiguous_range<R2> );
static_assert( std::ranges::sized_range<R2> );
R2 r2;
- std::wstring_view s2 = r2; // uses conversion to wstring_view
+ std::wstring_view s2(r2); // uses conversion to wstring_view
VERIFY( s2 == L"Out of range" );
VERIFY( std::wstring_view(const_cast<const R2&>(r2)) == s2 );
+ // And again using copy-initialization instead of direct-initialization.
+ std::wstring_view s2_implicit = r2; // uses conversion to wstring_view
+ VERIFY( s2_implicit == L"Out of range" );
+ VERIFY( std::wstring_view(const_cast<const R2&>(r2)) == s2_implicit );
+
struct R3 : R
{
using R::begin;
@@ -91,10 +96,10 @@ test01()
static_assert( std::ranges::contiguous_range<R5> );
static_assert( std::ranges::sized_range<R5> );
R5 r5;
- std::wstring_view s5 = r5; // Uses range constructor
+ std::wstring_view s5(r5); // Uses range constructor
VERIFY( s5 == r5.str );
s5 = std::wstring_view(std::move(r5)); // In C++20 this used conversion op.
- VERIFY( s5 == r5.str ); // In C++23 it uses range constructor.
+ VERIFY( s5 == r5.str ); // In C++23 it uses range constructor.
wchar_t arr[] = L"arrangement\0with\0nulls";
std::wstring_view sa = arr; // Does not use range constructor
@@ -156,15 +161,30 @@ test04()
};
R r;
- std::basic_string_view s = r; // Use deduction guide.
+ std::basic_string_view s(r); // Use deduction guide.
static_assert( std::is_same_v<decltype(s), std::wstring_view> );
}
+void
+test05()
+{
+ struct R
+ {
+ const wchar_t* begin() const { return nullptr; }
+ const wchar_t* end() const { return nullptr; }
+ };
+
+ // P2499R0 string_view range constructor should be explicit
+ // P2516R0 string_view is implicitly convertible from what?
+ static_assert( ! std::is_convertible_v<R, std::wstring_view> );
+}
+
int main()
{
test01();
test02();
test03();
test04();
+ test05();
}
--
2.37.1
reply other threads:[~2022-08-04 18:39 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220804183854.753948-1-jwakely@redhat.com \
--to=jwakely@redhat.com \
--cc=gcc-patches@gcc.gnu.org \
--cc=libstdc++@gcc.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).