Skip to content

Commit 7c0665d

Browse files
committed
fix: string_view_base noexcept on throwing operator std::string()
1 parent b55336c commit 7c0665d

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

include/boost/url/grammar/string_view_base.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ class string_view_base
159159
*/
160160
explicit
161161
operator
162-
std::string() const noexcept
162+
std::string() const
163163
{
164164
return std::string(s_);
165165
}

test/unit/grammar/string_view_base.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,40 @@ namespace boost {
1616
namespace urls {
1717
namespace grammar {
1818

19+
// Minimal derived class for testing
20+
struct test_string_view
21+
: string_view_base
22+
{
23+
explicit
24+
test_string_view(
25+
core::string_view s) noexcept
26+
: string_view_base(s)
27+
{
28+
}
29+
};
30+
1931
struct string_view_base_test
2032
{
2133
void
2234
run()
2335
{
36+
// operator std::string() allocates
37+
static_assert(
38+
!noexcept(std::string(
39+
std::declval<test_string_view const&>())),
40+
"operator std::string() must not be noexcept");
41+
42+
// Verify the conversion still works correctly
43+
{
44+
test_string_view sv("hello");
45+
std::string s = static_cast<std::string>(sv);
46+
BOOST_TEST(s == "hello");
47+
}
48+
{
49+
test_string_view sv("");
50+
std::string s = static_cast<std::string>(sv);
51+
BOOST_TEST(s.empty());
52+
}
2453
}
2554
};
2655

0 commit comments

Comments
 (0)