Bug 17242 – Specialized templates defined inside functions fail lookup, moving them outside makes them work

Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2017-03-03T15:41:06Z
Last change time
2022-11-18T09:53:12Z
Assigned to
No Owner
Creator
Andrei Alexandrescu

Comments

Comment #0 by andrei — 2017-03-03T15:41:06Z
This code causes a compilation error: void main () { template Floating1(T) if (is(T == cfloat) || is(T == cdouble) || is(T == creal)) { } template Floating1(T) if (is(T == float) || is(T == double) || is(T == real)) { } } Error: declaration Floating1(T) if (is(T == float) || is(T == double) || is(T == real)) is already defined Obviously moving the two templates to top level works. This is a violation of the "turtles all the way down" principle.
Comment #1 by bugzilla — 2017-03-03T19:56:51Z
It also is the case with nested functions: int foo(int i) { int bar(int a) { return a; } int bar(uint b) { return b; } return bar(i); } Name lookup is different inside functions. Overloading function definitions is not allowed, neither are forward references: int foo(int i) { return bar(i); int bar(int a) { return a; } }
Comment #2 by razvan.nitu1305 — 2022-11-18T09:53:12Z
Indeed, according to the spec [1] this is invalid. [1] https://dlang.org/spec/function.html#nested-declaration-order