https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113761
--- Comment #4 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Andrew Pinski from comment #2)
> It seems like libstdc++ miss that part, I wonder if it changed ...
No, it didn't change. We've just always had that bug. Well, not quite always,
looks like it was a regression in r0-94140-gf8dd9e0de06aef which added the
reserve call.
It's easy enough to fix:
--- a/libstdc++-v3/include/bits/random.tcc
+++ b/libstdc++-v3/include/bits/random.tcc
@@ -2877,10 +2877,10 @@ namespace __detail
param_type(initializer_list<_RealType> __bl, _Func __fw)
: _M_int(), _M_den(), _M_cp()
{
- _M_int.reserve(__bl.size());
- for (auto __biter = __bl.begin(); __biter != __bl.end(); ++__biter)
- _M_int.push_back(*__biter);
+ if (__bl.size() < 2)
+ return;
+ _M_int = __bl;
_M_den.reserve(_M_int.size() - 1);
for (size_t __k = 0; __k < _M_int.size() - 1; ++__k)
_M_den.push_back(__fw(0.5 * (_M_int[__k + 1] + _M_int[__k])));
But I don't understand why piecewise_constant_distribution is implemented this
way:
template<typename _RealType>
void
piecewise_constant_distribution<_RealType>::param_type::
_M_initialize()
{
if (_M_int.size() < 2
|| (_M_int.size() == 2
&& _M_int[0] == _RealType(0)
&& _M_int[1] == _RealType(1)))
{
_M_int.clear();
_M_den.clear();
return;
}
And then:
std::vector<_RealType>
intervals() const
{
if (_M_int.empty())
{
std::vector<_RealType> __tmp(2);
__tmp[1] = _RealType(1);
return __tmp;
}
else
return _M_int;
}
std::vector<double>
densities() const
{ return _M_den.empty() ? std::vector<double>(1, 1.0) : _M_den; }
This seems incredibly wasteful, why keep reallocating a new vector instead of
just populating _M_int with {1} and _M_den with {0,1}? Paolo did that in
r0-103705-g879b9073c87cea and it makes no sense to me, I'll see if the mailing
list has any clues.
Also, we're missing https://cplusplus.github.io/LWG/issue1439 which fixed the
return type of densities()