Skip to content

Commit a729718

Browse files
committed
🐛 Fix issue with size conversion
Problem: - When the sizes of two objects differ by an even number of bytes, `sized` is incorrect. Solution: - Fix it by checking `> 1` rather than `& 1`.
1 parent fd82b7b commit a729718

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

include/stdx/utility.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ template <typename T, typename U>
116116
if constexpr (sizeof(T) == sizeof(U)) {
117117
return sz;
118118
} else if constexpr (sizeof(T) > sizeof(U)) {
119-
return (sz * sizeof(T) / sizeof(U)) + (sizeof(T) % sizeof(U) & 1u);
119+
return (sz * sizeof(T) / sizeof(U)) + (sizeof(T) % sizeof(U) > 0);
120120
} else {
121121
return (sz * sizeof(T) + sizeof(U) - 1) / sizeof(U);
122122
}

test/utility.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,18 @@ TEST_CASE("sized<T> in (upsize not divisible)", "[utility]") {
147147
STATIC_REQUIRE(stdx::sized<T>{3}.in<std::uint32_t>() == 3);
148148
}
149149

150+
TEST_CASE("sized<T> in (downsize, mod > 1)", "[utility]") {
151+
using T = std::array<char, 6>;
152+
using U = std::array<char, 4>;
153+
STATIC_REQUIRE(stdx::sized<T>{1}.in<U>() == 2);
154+
}
155+
156+
TEST_CASE("sized<T> in (upsize, mod > 1)", "[utility]") {
157+
using T = std::array<char, 6>;
158+
using U = std::array<char, 4>;
159+
STATIC_REQUIRE(stdx::sized<U>{2}.in<T>() == 2);
160+
}
161+
150162
TEST_CASE("CX_VALUE structural value", "[utility]") {
151163
auto x = CX_VALUE(42);
152164
STATIC_REQUIRE(x() == 42);

0 commit comments

Comments
 (0)