Skip to content

Commit 07cc794

Browse files
committed
Safer addition
1 parent 529cb13 commit 07cc794

2 files changed

Lines changed: 25 additions & 15 deletions

File tree

lib/inc/sys_string/config.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,4 +151,11 @@
151151
#define SYS_STRING_HAS_TRAILING_REQUIRES_BUG 0
152152
#endif
153153

154+
155+
#if __cplusplus > 202302L && defined(__cpp_deleted_function) && __cpp_deleted_function >= 202403L
156+
#define SYS_STRING_DELETE_REASON(msg) = delete(msg)
157+
#else
158+
#define SYS_STRING_DELETE_REASON(msg) = delete
159+
#endif
160+
154161
#endif

lib/inc/sys_string/impl/addition.h

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -149,33 +149,36 @@ namespace sysstr::util
149149
m_second{right}
150150
{}
151151

152-
friend auto operator+(const addition & lhs, const sys_string_t<Storage> & rhs) noexcept
153-
{ return addition<Storage, addition, sys_string_t<Storage>>(lhs, rhs); }
154-
friend auto operator+(const addition & lhs, char32_t rhs) noexcept
155-
{ return addition<Storage, addition, char32_t>(lhs, rhs); }
152+
friend auto operator+(addition && lhs, const sys_string_t<Storage> & rhs) noexcept
153+
{ return addition<Storage, addition, sys_string_t<Storage>>(std::move(lhs), rhs); }
154+
friend auto operator+(addition & lhs, char32_t rhs) noexcept
155+
{ return addition<Storage, addition, char32_t>(std::move(lhs), rhs); }
156156
template <std::ranges::forward_range Range>
157-
friend auto operator+(const addition & lhs, const Range & rhs) noexcept
158-
{ return addition<Storage, addition, Range>(lhs, rhs); }
157+
friend auto operator+(addition && lhs, const Range & rhs) noexcept
158+
{ return addition<Storage, addition, Range>(std::move(lhs), rhs); }
159159

160-
friend auto operator+(const sys_string_t<Storage> & lhs, const addition & rhs) noexcept
161-
{ return addition<Storage, sys_string_t<Storage>, addition>(lhs, rhs); }
162-
friend auto operator+(char32_t lhs, const addition & rhs) noexcept
163-
{ return addition<Storage, char32_t, addition>(lhs, rhs); }
160+
friend auto operator+(const sys_string_t<Storage> & lhs, addition && rhs) noexcept
161+
{ return addition<Storage, sys_string_t<Storage>, addition>(lhs, std::move(rhs)); }
162+
friend auto operator+(char32_t lhs, addition && rhs) noexcept
163+
{ return addition<Storage, char32_t, addition>(lhs, std::move(rhs)); }
164164
template <std::ranges::forward_range Range>
165-
friend auto operator+(const Range & lhs, const addition & rhs) noexcept
166-
{ return addition<Storage, Range, addition>(lhs, rhs); }
165+
friend auto operator+(const Range & lhs, addition && rhs) noexcept
166+
{ return addition<Storage, Range, addition>(lhs, std::move(rhs)); }
167167

168168
template<class RHSFirst, class RHSSecond>
169-
friend auto operator+(const addition & lhs, const addition<Storage, RHSFirst, RHSSecond> & rhs) noexcept
170-
{ return addition<Storage, addition, addition<Storage, RHSFirst, RHSSecond>>(lhs, rhs); }
169+
friend auto operator+(addition && lhs, addition<Storage, RHSFirst, RHSSecond> && rhs) noexcept
170+
{ return addition<Storage, addition, addition<Storage, RHSFirst, RHSSecond>>(std::move(lhs), std::move(rhs)); }
171171

172-
operator sys_string_t<Storage>() const
172+
operator sys_string_t<Storage>() const &&
173173
{
174174
sys_string_builder_t<Storage> builder;
175175
builder.reserve_storage(this->storage_size());
176176
this->append_to(builder);
177177
return builder.build();
178178
}
179+
180+
operator sys_string_t<Storage>() const & SYS_STRING_DELETE_REASON(
181+
"you must convert the result of sys_string_t addition to sys_string_t. Do not assign it to auto and carry around");
179182

180183
auto storage_size() const -> typename sys_string_t<Storage>::size_type
181184
{

0 commit comments

Comments
 (0)