On Sunday, 23 September 2012 at 04:03:28 UTC, Jonathan M Davis
wrote:
On Sunday, September 23, 2012 05:49:06 Craig Dillabaugh wrote:
Hello,

clip


Before anything, I'd question why you declared vt at all. If all you're putting in it is a single struct, then just templatize the struct directly:

struct Vertex(T)
{
    ...
}

Now, it looks like you have a free function in there as well - euclid_dist - but there's no reason to put that in the same template. Just templatize it
directly. Then you get

T euclid_dist(T)(T a, T b) {...}

And main ends up looking something like

void main()
{
    auto v1 = Vertex!float(0.0, 0.0);
    auto v2 = Vertex!float(2.0, 4.0);
    writefln("The distance between vertex 1 and vertex 2 is %s",
             euclid_dist(v1, v2));
}
Thanks. I've switched my code to follow your suggestion. One thing
there was a typo in my euclid_dist function, the code for it, and
main now look like:

T euclid_dist(T)(vertex!T a, vertex!T b) {
        T sum = 0;
        foreach( ref a_crd, ref b_crd; lockstep( a.coords, b.coords ) ) {
                sum += (a_crd - b_crd)*(a_crd - b_crd );
        }
        return sqrt(sum);
}

int main(string argv[] ) {
        auto v1 = vertex!float(0.0,0.0);
        auto v2 = vertex!float(3.0,4.0);
        writeln("The distance between vertex 1 and vertex 2 is ",
                euclid_dist!float(v1, v2) );
        return 0;
}

One question. Is there any way to get the function template to
deduce the type of T from the vertices I pass, so that I can
call:

euclid_dist(v1, v2) )

instead of:

euclid_dist!float(v1, v2) );


It's actually fairly to explicitly declare a template in D outside of eponymous templates. If you're dealing with a user-defined type or function, it
almost always makes more sense to templatize them directly.

Now, as for the exact error message, it's because the syntax that you're using
to define v1 and v2 is illegal. You'd need to do either

vt!float.vertex v1 = vt!float(0.0, 0.0);

or preferrably.

auto v1 = vt!float(0.0, 0.0);

You can't construct the type on the left-hand side of the assignment operator
like that.

- Jonathan M Davis

,

Reply via email to