Bug 18917 – Default Value for Function-Type Template Value-Parameter Causes Conflicts in Instantiation

Status
RESOLVED
Resolution
DUPLICATE
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2018-05-30T09:36:17Z
Last change time
2022-01-28T09:56:41Z
Keywords
link-failure, rejects-valid
Assigned to
No Owner
Creator
Vijay Nayar

Comments

Comment #0 by madric — 2018-05-30T09:36:17Z
Consider the following code snippet of a template class that has 3 paramters, the 3rd of which is a value-parameter with a function type and has a default value. ``` final class BTree( ValueT, KeyT = ValueT, const(KeyT) function(ValueT) nothrow pure @nogc KeyF = function KeyT(ValueT a) { return a; }) { KeyT getKey(ValueT val) { return KeyF(val); } } ``` When instantiating these templates, the first instantiation sets the value for the function value-parameter, and subsequent instantiations fail due to mismatching type. Example: ``` void main() { auto btree1 = new BTree!(char); auto btree2 = new BTree!(int); // The error is on this line. } ``` The error is: ``` onlineapp.d(17): Error: template instance `BTree!int` does not match template declaration `BTree(ValueT, KeyT = ValueT, const(char) function(char) pure nothrow @nogc KeyF = function KeyT(ValueT a) { return a; } )` ``` I believe that `BTree!(char)` and `BTree!(int)` should be recognized as different types at compile-time that have separate and distinct default values for the 3rd template-parameter.
Comment #1 by trikkuz — 2018-05-30T10:18:06Z
Reduced: class A(T, T function(T) func = function(T a) { return a; }) { } void main() { A!char a; A!int b; }
Comment #2 by madric — 2018-08-23T10:28:19Z
Any confirmation on whether this is a legitimate bug or if the intended behavior is that default values for template parameters are meant to be instantiated only once and cause conflicts for future instantiations of the same template?
Comment #3 by greeenify — 2018-08-23T22:02:01Z
Definitely looks like a bug to me.
Comment #4 by razvan.nitu1305 — 2022-01-28T09:56:41Z
*** This issue has been marked as a duplicate of issue 22540 ***