Unwanted recursive expansion

Daniel Kozák via Digitalmars-d digitalmars-d at puremagic.com
Thu Jan 15 05:14:27 PST 2015


V Thu, 15 Jan 2015 12:31:00 +0000
"Luc J. Bourhis via Digitalmars-d" <digitalmars-d at puremagic.com>
napsáno:

> Consider the following program:
> 
> struct Expr(E) {
>    const E expr;
> 
>    // If I replace the return type with "auto", dmd enters into an 
> infinite loop instead of failing (see below)
>    @property Expr!(Transposed!E)  trans() {
>      alias R = Transposed!E;
>      return Expr!R(R(expr));
>    }
> }
> 
> struct Transposed(E) {
>      const E expr;
> }
> 
> struct Matrix {
>    @property auto asExpression() {
>      return Expr!(Matrix)(this);
>    }
> 
>    alias asExpression this;
> }
> 
> void main() {
>      auto a = Matrix();
>      auto e = a.trans;
>      pragma(msg, typeof(e).stringof);
> }
> 
> It fails to compile:
> 
> ~> dmd -v | head -n1
> DMD64 D Compiler v2.066-devel
> ~> dmd -c expression_template.d
> expression_template.d(4): Error: template instance 
> expression_template.Transposed!(Transposed!(Transposed!(Transposed!( 
> .................. ) recursive expansion
> 
> Am I missing something?
> 



This is ok, there is really recursive expansion here, I guess you want
something like this?

struct Expr(E) {
    const E expr;

    @property auto trans() {
        alias R = Transposed!E;
        return R(expr);
    }
}

struct Transposed(E) {
    const E expr;
}

struct Matrix {
    @property auto asExpression() {
        return Expr!(Matrix)(this);
    }
    
    alias asExpression this;
}

void main() {
    auto a = Matrix();
    auto e = a.trans;
    pragma(msg, typeof(e).stringof);
}



More information about the Digitalmars-d mailing list