forward references... again?

Mehrdad wfunction at hotmail.com
Mon Sep 26 09:05:47 PDT 2011


Hm... why is this giving a forward reference error?

int main()
{
     auto a = normal(5);
     auto b = a + a;
     writeln(b.get(0.0));
}

struct OpBinary(string op, T...) if (T.length == 2)
{
     T values;

     this(T values) { this.values = values; }

     auto ref get(T2...)(T2 args)
     {
         static assert(T.length == 2);
         return (mixin(q{this.values[0]} ~ op ~ 
q{this.values[1]})).get(args);   // Why is calling get() considered a 
"forward reference"?
     }
}

struct Normal(T = real)
{
     T mean, stddev;

     this(T mean = 0, T stddev = 0) { this.mean = mean; this.stddev = 
stddev; }

     auto get(T2)(T2 value)
     {
         auto variance = this.stddev * this.stddev;
         auto offset = value - this.mean;
         return variance == 0.0 ? this.mean == value : 
.std.math.exp(-(offset * offset) / (2.0 * variance)) / 
.std.math.sqrt(2.0 * PI * variance);
     }

     OpBinary!(op, typeof(this), T2) opBinary(string op, T2)(T2 other) const
         if (op == "+" || op == "-" || op == "*" || op == "/")
     {
         return typeof(return)(this, other);
     }
}

auto normal(T)(T value) { real v = value; return Normal!(T)(v); }


More information about the Digitalmars-d mailing list