www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 11452] New: template alias doesn't work with default template arguments

reply d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=11452

           Summary: template alias doesn't work with default template
                    arguments
           Product: D
           Version: D2
          Platform: x86_64
        OS/Version: Linux
            Status: NEW
          Severity: minor
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: atila.neves gmail.com


--- Comment #0 from Atila Neves <atila.neves gmail.com> 2013-11-06 05:39:16 PST
---
template_alias.d:

struct Foo(uint LENGTH, T = bool) {
}

template Foos(uint SIZE, T = bool) {
    alias Foo!(SIZE, T)[] Foos;
}


void func1(uint num, uint size)(Foo!(size)[]) {
}

void func2(uint num, uint size)(Foos!(size)) {
}

void main() {
    Foo!(4)[] foos;
    func1!(2)(foos); //compiles
    func2!(2)(foos); //doesn't compile
}

dmd template_alias.d
template_alias.d(18): Error: template template_alias.func2 does not match any
function template declaration. Candidates are:
template_alias.d(12):        template_alias.func2(uint num, uint
size)(Foos!(size))
template_alias.d(18): Error: template template_alias.func2(uint num, uint
size)(Foos!(size)) cannot deduce template function from argument types
!(3)(Foo!(4)[])
template_alias.d(18): Error: template instance func2!(3) errors instantiating
template

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 06 2013
next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=11452


Dicebot <public dicebot.lv> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |public dicebot.lv


--- Comment #1 from Dicebot <public dicebot.lv> 2013-11-06 05:54:06 PST ---
Reduced test case:

template Foos(uint SIZE)
{
    alias Foos = int[SIZE];
}

void func(uint size)(Foos!size) {}

void main()
{
    int[4] foos;
    func!4(foos); // does compile
    func(foos);   // doesn't compile
}

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 06 2013
prev sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=11452


Simen Kjaeraas <simen.kjaras gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |simen.kjaras gmail.com


--- Comment #2 from Simen Kjaeraas <simen.kjaras gmail.com> 2013-11-06 08:45:06
PST ---
I'm not sure this is something we actually do want. Consider:

template Foos(uint size, T) {
    static if (is(T == string)) {
        alias Foos = T[size*3];
    } else {
        alias Foos = T[size];
    }
}

void func(uint size, T)(Foos!(size, T)) {
}

void test() {
    string[6] a;
    func(a);
}


How is the poor compiler to figure out the values of size and T? An even
simpler example:

template Foos(uint size, T) {
    alias Foos = float;
}


Bottom line is, templates can be arbitrarily complex. This sort of matching can
only be done for very simple cases.

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 06 2013