Here is a "funny" bug with variadic function taking float arguments, whenever foo takes the float parameter and Vector2 is a larger type then the second assert fails.
It works as expected when foo and Vector2 is both float or double, or foo is not float.
There is a strange behavior in the foo as well.
Note that this only happens with DMD, with LDC it works correctly.
This code is tested on Windows.
Reproduction code
------------------------------
alias real_t = double;
struct Vector2 {
real_t x, y;
this(real_t x, real_t y) { this.x = x; this.y = y; }
}
Vector2 foo(size_t nArgCount)(float[nArgCount] args...)
{
//import std.stdio;
//writeln(args); // [0.5]
//writeln(args[0]); // 0
//assert(args[0] == 0.5f); // assert failed
return Vector2(args[0], args[0]);
}
void main()
{
// ok
auto c = foo(0.5, 0.5);
assert(c.x == 0.5 && c.y == 0.5);
// Note that it is just by coincidence that the stack is zeroed,
// in real program there will be garbage values.
// This bug happens ONLY when foo type is float and Vector2 any other type.
// It works properly with LDC, but not with DMD.
import std.stdio;
c = foo(0.5);
writefln("%f, %f", c.x, c.y); // 0.000000, 0.000000
assert(c.x == 0.5 && c.y == 0.5);
}
Comment #1 by robert.schadek — 2024-12-13T19:30:58Z