diff options
author | Lenny Maiorani <lenny@colorado.edu> | 2018-11-03 21:15:05 -0600 |
---|---|---|
committer | Lenny Maiorani <lenny@colorado.edu> | 2018-11-04 20:50:40 -0700 |
commit | 76e13b586ff690dd3312f719f305c0d1021cd505 (patch) | |
tree | 04fa746cd6c9b2cb088895df4ebe8144dfd5701d | |
parent | 742ee213499194f97e59dae4971f1474ae7d57ad (diff) |
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()`.
-rw-r--r-- | src/prevector.h | 16 |
1 files changed, 3 insertions, 13 deletions
diff --git a/src/prevector.h b/src/prevector.h index 6ddb6f321f..aa77573746 100644 --- a/src/prevector.h +++ b/src/prevector.h @@ -10,6 +10,7 @@ #include <stdint.h> #include <string.h> +#include <algorithm> #include <cstddef> #include <iterator> #include <type_traits> @@ -198,22 +199,11 @@ private: 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> |