D's limited template specialization abilities compared to C++

Kenji Hara k.hara.pg at gmail.com
Sat May 25 06:47:36 PDT 2013


2013/5/25 Ahuzhgairl <bulletproofchest at gmail.com>

> No,
>
> struct Foo(T) {
>     static void f() { writeln("general"); }
> }
>
> struct Foo(T : A(B).alias C, A, B, C) {
>     static void f() { writeln("special"); }
> }
>
> struct Bar(T) {
>     struct Baz {}
> }
>
> struct Baz(T : A(B), A, B) {
> }
>
> void main() {
>     Foo!(Bar!(int).Baz);
>     Baz!(Bar!(int));
> }
>

As I already shown, Baz!(Bar!(int)); could work in D.

But, currently Foo!(Bar!(int).Baz); is not yet supported.

I'm opening a compiler enhancement for related case,
http://d.puremagic.com/issues/show_bug.cgi?id=9022

and right now I updated compiler patch to allow parameterize enclosed type
by name/type/alias.
https://github.com/D-Programming-Language/dmd/pull/1296
https://github.com/9rnsr/dmd/commit/b29726d30b0094b9e7c2e15f5802501cb686ee68

After it is merged, you can write it as follows.

import std.stdio;
struct Foo(T)
{
    static void f() { writeln("general"); }
}
struct Foo(T : A!(B).C, alias A, B, alias C)
{
    static void f() { writeln("special"); }
}

struct Bar(T) { struct Baz {} }

void main()
{
    Foo!(Bar!(int).Baz) x;
    x.f();  // prints "special"
}

Kenji Hara
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20130525/f1e13bef/attachment-0001.html>


More information about the Digitalmars-d mailing list