Skip to content

Commit

Permalink
warnings: Compiler warning on memset usage for non-trivial type
Browse files Browse the repository at this point in the history
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()`.
  • Loading branch information
ldm5180 committed Nov 5, 2018
1 parent 742ee21 commit 76e13b5
Showing 1 changed file with 3 additions and 13 deletions.
16 changes: 3 additions & 13 deletions src/prevector.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <stdint.h>
#include <string.h>

#include <algorithm>
#include <cstddef>
#include <iterator>
#include <type_traits>
Expand Down Expand Up @@ -198,22 +199,11 @@ class prevector {
const T* item_ptr(difference_type pos) const { return is_direct() ? direct_ptr(pos) : indirect_ptr(pos); }

void fill(T* dst, ptrdiff_t count) {
if (IS_TRIVIALLY_CONSTRUCTIBLE<T>::value) {
// The most common use of prevector is where T=unsigned char. For
// trivially constructible types, we can use memset() to avoid
// looping.
::memset(dst, 0, count * sizeof(T));
} else {
for (auto i = 0; i < count; ++i) {
new(static_cast<void*>(dst + i)) T();
}
}
std::fill_n(dst, count, T{});
}

void fill(T* dst, ptrdiff_t count, const T& value) {
for (auto i = 0; i < count; ++i) {
new(static_cast<void*>(dst + i)) T(value);
}
std::fill_n(dst, count, value);
}

template<typename InputIterator>
Expand Down

0 comments on commit 76e13b5

Please sign in to comment.