This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
Re: valarray_copy implementation question
Paolo Carlini wrote:
> Thanks Gaby, because in fact I'm finding the issue quite substantive,
> isn't just about some strange corner cases: it's really about copying
> all the source elements or just picking some depending on the
> destination mask; it's about checking for overruns of the destination or
> not. A big difference.
>
To better explain what I mean, the below snippet, perfectly legal as far
as I can see and not triggering any memory error, would fail the third
assert at runtime if we do implement the simple change suggested by
Christian. Note, the alternate implementation passes the testcase, as
current libstdc++ does.
I'm wondering if shouldn't just keep the current implementation
unchanged, besides adding to it a check that the destination is not
overrun. But that is also debatable, given the usual
performance-dictated design choices for valarray...
Paolo.
///////////////////
#include <valarray>
#include <cassert>
int main()
{
int n = 6;
std::valarray<int> dest (n);
std::valarray<bool> b (false, n);
b[2] = b[4] = b[5] = true;
std::valarray<int> src (3);
src[0] = -1;
src[1] = -2;
src[2] = -3;
dest[b] = src; // 0
assert ( dest[0] == 0 );
assert ( dest[1] == 0 );
assert ( dest[2] == -1 );
assert ( dest[3] == 0 );
assert ( dest[4] == -2 );
assert ( dest[5] == -3 );
}