Skip to content

Commit c8dbbcc

Browse files
authored
Merge pull request #192 from elbeno/fix-sized-bug
🐛 Fix `sized` issue with indivisible sizes
2 parents 1f7d93a + f03298e commit c8dbbcc

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-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));
119+
return (sz * sizeof(T) / sizeof(U)) + (sizeof(T) % sizeof(U) & 1u);
120120
} else {
121121
return (sz * sizeof(T) + sizeof(U) - 1) / sizeof(U);
122122
}

test/utility.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <catch2/catch_template_test_macros.hpp>
44
#include <catch2/catch_test_macros.hpp>
55

6+
#include <array>
67
#include <cstdint>
78
#include <string_view>
89
#include <type_traits>
@@ -130,6 +131,18 @@ TEST_CASE("sized<T> aliases", "[utility]") {
130131
static_assert(stdx::sized64{1}.in<std::uint64_t>() == 1);
131132
}
132133

134+
TEST_CASE("sized<T> in (downsize not divisible)", "[utility]") {
135+
using T = std::array<char, 3>;
136+
static_assert(sizeof(T) == 3);
137+
static_assert(stdx::sized<std::uint32_t>{2}.in<T>() == 3);
138+
}
139+
140+
TEST_CASE("sized<T> in (upsize not divisible)", "[utility]") {
141+
using T = std::array<char, 3>;
142+
static_assert(sizeof(T) == 3);
143+
static_assert(stdx::sized<T>{3}.in<std::uint32_t>() == 3);
144+
}
145+
133146
TEST_CASE("CX_VALUE structural value", "[utility]") {
134147
auto x = CX_VALUE(42);
135148
static_assert(x() == 42);

0 commit comments

Comments
 (0)