Bug 19572 – std.array.Appender.put invokes struct constructor via cast
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2019-01-10T14:27:48Z
Last change time
2019-03-29T13:52:14Z
Assigned to
No Owner
Creator
FeepingCreature
Comments
Comment #0 by default_357-line — 2019-01-10T14:27:48Z
Repro:
struct Struct
{
private int key_;
public int fun() const { return 23; }
alias fun this;
}
void main()
{
import std.array : Appender;
Appender!(Struct[]) appender;
appender.put(const(Struct)(42));
auto result = appender.data[0];
assert(result.key_ != 23);
}
What happens is that Appender checks that it can convert const(Struct) implicitly to Struct, but it does so via cast(Struct), which then invokes Struct(const(Struct).fun) implicitly.
Appender should cast() its parameter, then rely on implicit conversion to do the rest.
Comment #1 by default_357-line — 2019-03-29T13:52:14Z
Fixed in 2.085.0: constness-casts no longer implicitly call the struct constructor.