Skip to content

Commit 76e13b5

Browse files
committed
warnings: Compiler warning on memset usage for non-trivial type
Problem: - IS_TRIVIALLY_CONSTRUCTIBLE macro does not work correctly resulting in `memset()` usage to set a non-trivial type to 0 when `nontrivial_t` is passed in from the tests. - Warning reported by GCC when compiling with `--enable-werror`. Solution: - Use the standard algorithm `std::fill_n()` and let the compiler determine the optimal way of looping or using `memset()`.
1 parent 742ee21 commit 76e13b5

File tree

1 file changed

+3
-13
lines changed

1 file changed

+3
-13
lines changed

src/prevector.h

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <stdint.h>
1111
#include <string.h>
1212

13+
#include <algorithm>
1314
#include <cstddef>
1415
#include <iterator>
1516
#include <type_traits>
@@ -198,22 +199,11 @@ class prevector {
198199
const T* item_ptr(difference_type pos) const { return is_direct() ? direct_ptr(pos) : indirect_ptr(pos); }
199200

200201
void fill(T* dst, ptrdiff_t count) {
201-
if (IS_TRIVIALLY_CONSTRUCTIBLE<T>::value) {
202-
// The most common use of prevector is where T=unsigned char. For
203-
// trivially constructible types, we can use memset() to avoid
204-
// looping.
205-
::memset(dst, 0, count * sizeof(T));
206-
} else {
207-
for (auto i = 0; i < count; ++i) {
208-
new(static_cast<void*>(dst + i)) T();
209-
}
210-
}
202+
std::fill_n(dst, count, T{});
211203
}
212204

213205
void fill(T* dst, ptrdiff_t count, const T& value) {
214-
for (auto i = 0; i < count; ++i) {
215-
new(static_cast<void*>(dst + i)) T(value);
216-
}
206+
std::fill_n(dst, count, value);
217207
}
218208

219209
template<typename InputIterator>

0 commit comments

Comments
 (0)