Skip to content

Commit

Permalink
[struct_pack] fix msvc assert failed when string resize (#631)
Browse files Browse the repository at this point in the history
  • Loading branch information
poor-circle authored Mar 17, 2024
1 parent a2b65ab commit 06d28e6
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions include/ylt/struct_pack/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,6 @@ void string_set_length_hacker(std::string &, std::size_t);

template <typename ch>
inline void resize(std::basic_string<ch> &raw_str, std::size_t sz) {
std::string &str = *reinterpret_cast<std::string *>(&raw_str);
#if defined(_GLIBCXX_USE_CXX11_ABI)
constexpr bool is_use_cxx11_abi = _GLIBCXX_USE_CXX11_ABI;
#else
Expand All @@ -204,14 +203,15 @@ inline void resize(std::basic_string<ch> &raw_str, std::size_t sz) {
defined(_MSVC_STL_VERSION)
if constexpr (is_string_reserve_shrink) {
if (sz > raw_str.capacity()) {
str.reserve(sz * sizeof(ch));
raw_str.reserve(sz);
}
}
else {
str.reserve(sz * sizeof(ch));
raw_str.reserve(sz);
}
std::string &str = *reinterpret_cast<std::string *>(&raw_str);
string_set_length_hacker(str, sz);
for (auto i = sz; i < sz + sizeof(ch); ++i) str[i] = '\0';
*(raw_str.data() + sz) = 0;
#else
raw_str.resize(sz);
#endif
Expand Down

0 comments on commit 06d28e6

Please sign in to comment.