libstdc++: Make std::string_view(Range&&) constructor explicit
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.
This commit is contained in:
parent
db33daa467
commit
2678386df2
3 changed files with 50 additions and 10 deletions
|
@ -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))
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue