Vararg-templated class with matching member func

bearophile bearophileHUGS at lycos.com
Sat Apr 6 16:53:34 PDT 2013


Nick Sabalausky:

> I have a feeling I'm missing something obvious, but how do I 
> create
> something like this?:
>
> class Foo(TArgs...)
> {
>     void func(/+ args must exactly match TArgs +/)
>     {
>     }
> }
>
> new Foo(int).func(5); // Ok
> new Foo(int, string).func(5, "hello"); // Ok
>
> // Error! Overload not found!
> new Foo(int).func(5, 8);
> new Foo(int).func("hellO");
>
> // Error! Overload not found!
> new Foo(int, string).func(5);
> new Foo(int, string).func("hello");
>
> Maybe it's just a slow-brain day, but I'm having trouble 
> working out
> the right syntax for that.

Do you mean something like this?



class Foo(TArgs...) {
     void func(TArgs args) {}
}

void main() {
     (new Foo!(int)).func(5); // Ok
     (new Foo!(int, string)).func(5, "hello"); // Ok

     // Error! Overload not found!
     (new Foo!(int)).func(5, 8);
     (new Foo!(int)).func("hellO");

     // Error! Overload not found!
     (new Foo!(int, string)).func(5);
     (new Foo!(int, string)).func("hello");
}


The extra () around the new is something that we are asking to be 
not required. But Walter for unknown reasons seems to not like 
the idea...

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list