https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109150
Bug ID: 109150
Summary: std::fill should use __gnu_cxx::__is_scalar overloads
for all scalars
Product: gcc
Version: 13.0
Status: UNCONFIRMED
Keywords: missed-optimization
Severity: normal
Priority: P3
Component: libstdc++
Assignee: unassigned at gcc dot gnu.org
Reporter: redi at gcc dot gnu.org
Target Milestone: ---
std::fill and std::fill_n have optimized implementations for simple types, but
dispatching on std::__is_scalar which is not true for enumeration types or
pointer-to-member types.
template<typename _ForwardIterator, typename _Tp>
_GLIBCXX20_CONSTEXPR
inline typename
__gnu_cxx::__enable_if<__is_scalar<_Tp>::__value, void>::__type
__fill_a1(_ForwardIterator __first, _ForwardIterator __last,
const _Tp& __value)
{
const _Tp __tmp = __value;
for (; __first != __last; ++__first)
*__first = __tmp;
}
Where __is_scalar doesn't match the semantics of std::is_scalar, but rather:
template<typename _Tp>
struct __is_scalar
: public __traitor<__is_arithmetic<_Tp>, __is_pointer<_Tp> >
{ };
We should enable this optimization for more types. Maybe use the same set of
types matches by std::is_scalar? Or all types that are trivially copy
constructible and trivially copy assignable?
I think the point of the optimization is so the compiler knows that __tmp
doesn't alias anything in the iterator range. But if the iterators value type
is not _Tp, this optimization might actually be wrong (the value type's
assignment operator might care about the identity of _Tp).
Review this, decide whether the traits in cpp_type_traits.h make sense to keep,
or should be reimplemented in terms of <type_traits> and/or built-ins.