Vararg-templated class with matching member func
Nick Sabalausky
SeeWebsiteToContactMe at semitwist.com
Sat Apr 6 19:36:14 PDT 2013
On Sun, 07 Apr 2013 01:53:34 +0200
"bearophile" <bearophileHUGS at lycos.com> wrote:
> 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...
>
Actually, the missing parens around new were just part of my example,
not in my original code.
The "void func(TArgs args) {}" is something I had tried, but apparently
my real problem was something else:
class Foo(TArgs...) {
void func(TArgs args) { func2(args); }
void func2()(TArgs args) {}
}
void main() {
(new Foo!(int)).func(5);
}
That fails. If func2 is *not* a template then it works. Looks like I
stumbled on a compiler bug. And idea offhand whether it's a known one?
I'm going to go ahead and file it, feel free to close if you know
it's a duplicate (I didn't see anything like it though):
http://d.puremagic.com/issues/show_bug.cgi?id=9894
More information about the Digitalmars-d-learn
mailing list