[Issue 24827] New: maxElement does not correctly handle types with opAssign
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Wed Oct 23 05:49:32 UTC 2024
https://issues.dlang.org/show_bug.cgi?id=24827
Issue ID: 24827
Summary: maxElement does not correctly handle types with
opAssign
Product: D
Version: D2
Hardware: All
OS: All
Status: NEW
Severity: normal
Priority: P1
Component: phobos
Assignee: nobody at puremagic.com
Reporter: issues.dlang at jmdavisProg.com
Strictly speaking, I'm pretty sure that the problem here is that Rebindable2
doesn't use emplaceCopy but instead uses =, but maxElement is the public symbol
which exposes this issue, an example being
---
void main()
{
import std.algorithm.searching : maxElement;
auto arr = [S(19), S(2), S(145), S(7)];
assert(maxElement(arr) == S(145));
}
struct S
{
int i;
bool destroyed;
this(int i)
{
this.i = i;
}
~this()
{
destroyed = true;
}
bool opEquals()(auto ref S rhs)
{
return this.i == rhs.i;
}
int opCmp()(auto ref S rhs)
{
if(this.i < rhs.i)
return -1;
return this.i == rhs.i ? 0 : 1;
}
invariant
{
assert(!destroyed);
}
}
---
This results is the assertion in the invariant failing a bunch of times.
--
More information about the Digitalmars-d-bugs
mailing list