Allow slice null ptr safely if slice is empty

This commit is contained in:
David Tolnay 2021-03-28 02:00:02 -04:00
parent 96ee470356
commit bb98d9eb09
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82

View File

@ -505,8 +505,12 @@ Slice<T>::Slice() noexcept {
template <typename T>
Slice<T>::Slice(T *s, std::size_t count) noexcept {
assert(s != nullptr);
sliceInit(this, const_cast<typename std::remove_const<T>::type *>(s), count);
assert(s != nullptr || count == 0);
sliceInit(this,
s == nullptr && count == 0
? reinterpret_cast<void *>(align_of<T>())
: const_cast<typename std::remove_const<T>::type *>(s),
count);
}
template <typename T>