Bug 12620 – Compiler picks lesser template specialization match for float array alias value parameters
Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2014-04-22T22:53:00Z
Last change time
2017-07-03T22:27:31Z
Assigned to
nobody
Creator
andrej.mitrovich
Comments
Comment #0 by andrej.mitrovich — 2014-04-22T22:53:26Z
-----
template Foo(alias sym) { pragma(msg, "Foo1"); }
template Foo(alias int[] sym) { pragma(msg, "Foo2"); }
template Foo(alias float[] sym) { pragma(msg, "Foo3"); }
void main()
{
alias foo1 = Foo!(1); // instantiates #1, ok
alias foo2 = Foo!([1]); // instantiates #2, ok
alias foo3 = Foo!([1.0]); // instantiates #1 instead of #3!
}
-----
Comment #1 by andrej.mitrovich — 2014-04-23T21:37:19Z
(In reply to Andrej Mitrovic from comment #0)
> -----
> template Foo(alias sym) { pragma(msg, "Foo1"); }
> template Foo(alias int[] sym) { pragma(msg, "Foo2"); }
> template Foo(alias float[] sym) { pragma(msg, "Foo3"); }
>
> void main()
> {
> alias foo1 = Foo!(1); // instantiates #1, ok
> alias foo2 = Foo!([1]); // instantiates #2, ok
> alias foo3 = Foo!([1.0]); // instantiates #1 instead of #3!
> }
> -----
Changing the third overload to use double[] instead of float[] fixes this. So it must be some issue with a missed implicit conversion.
Comment #2 by markus — 2015-01-25T10:24:17Z
Not a bug. You must use 1.0f (note the "f") if you want a float constant.
alias foo3 = Foo!([1.0f]);
Comment #3 by andrej.mitrovich — 2015-01-25T11:57:36Z
(In reply to mfx from comment #2)
> Not a bug. You must use 1.0f (note the "f") if you want a float constant.
>
> alias foo3 = Foo!([1.0f]);
Ah, could be. I guess it makes sense to prefer matching the version which retains the matching type (and retains the full range of the type this way) rather than tries to implicitly convert it.
I'll CC Kenji on his opinions.
Comment #4 by dlang-bugzilla — 2017-07-03T20:33:08Z
(In reply to Andrej Mitrovic from comment #3)
> Ah, could be. I guess it makes sense to prefer matching the version which
> retains the matching type (and retains the full range of the type this way)
> rather than tries to implicitly convert it.
Err, but you can't implicitly convert a double[] to a float[].
I mean, it works when you assign it:
float[] arr = [1.0];
So does with int->byte:
byte[] arr = [1];
but int->byte conversion fails with your example too:
template Foo(alias sym) { pragma(msg, "Foo1"); }
template Foo(alias byte[] sym) { pragma(msg, "Foo2"); }
void main()
{
alias foo1 = Foo!([1]); // prints Foo1
}
So, this issue is either invalid, or it's about an enhancement request to extend the same type of implicit conversion used for initialization / literal assignment to alias parameters.
Seeing how this issue as initially formulated was based on a misunderstanding, and that it was filed 3 years ago, I'll be closing this, but feel free to reopen if you disagree.
Comment #5 by andrej.mitrovich — 2017-07-03T22:27:31Z
Jesus sorry to spam Vladimir, but just how many issues have I filed over the years? lol.
Thanks for taking care of this.