https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94933
Bug ID: 94933
Summary: std::fill_n delegates to __builtin_memset which is not
constexpr
Product: gcc
Version: 10.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: libstdc++
Assignee: unassigned at gcc dot gnu.org
Reporter: rhalbersma at gmail dot com
Target Milestone: ---
std::fill_n is constexpr in C++20 but when writing unsigned chars it delegates
to __builtin_memset which is not constexpr:
#include <algorithm>
template<int N, class T>
struct S {
T data[N]{};
constexpr auto& clear() {
std::fill_n(std::begin(data), N, T{0});
return *this;
}
};
int main()
{
using T = S<1, unsigned char>;
constexpr auto b = T{}.clear();
static_assert(b.data[0] == 0);
}
Tested on Wandbox:
https://wandbox.org/permlink/xRU9JUQopzEzVuUg
Works correctly for Clang + libc++ >= 9.
I haven't checked whether similar bugs appear for other algorithms which can be
optimized for chars (std::equal -> memcmp etc.).
A possible fix is to use std::is_constand_evaluated() to make a
constexpr-friendly implementation at compile-time and __builtin_memset at
run-time.