<div class="gmail_quote">2011/7/27 Steven Schveighoffer <span dir="ltr"><<a href="mailto:schveiguy@yahoo.com">schveiguy@yahoo.com</a>></span><br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div><div></div><div class="h5">On Wed, 27 Jul 2011 15:48:24 -0400, Asger Dam Hoedt <<a href="mailto:asgerhoedt@gmail.com" target="_blank">asgerhoedt@gmail.com</a>> wrote:<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hey<br>
<br>
I've very recently started playing around with D, but I seem to have hit my<br>
head against a bug in dmd. I want to create a Vector class templated with<br>
its dimension. This works fine until I try to overload the + operator for my<br>
vector and use it. I then get the error<br>
<br>
Error: incompatible types for ((vec) + (vec)): 'Vector!(3)' and 'Vector!(3)'<br>
<br>
I've boiled it down to a small example<br>
<br>
struct Vector(int D) {<br>
    float es[D];<br>
<br>
public:<br>
    this(immutable float x, immutable float y, immutable float z) {<br>
        es[0] = x; es[1] = y; es[2] = z;<br>
    }<br>
<br>
    Vector!(D) opBinary(string s)(immutable Vector!(D) rhs) if (s == "+") {<br>
        Vector!(D) ret;<br>
        for(int i = 0; i < D; ++i)<br>
            <a href="http://ret.es" target="_blank">ret.es</a>[i] = es[i] + <a href="http://rhs.es" target="_blank">rhs.es</a>[i];<br>
        return ret;<br>
    }<br>
<br>
}<br>
<br>
alias Vector!(3) Vector3;<br>
<br>
void main() {<br>
    Vector3 vec = Vector3(0,1,2);<br>
    vec = vec.opBinary!("+")(vec); // This works fine<br>
    vec = vec + vec; // This line fails miserably<br>
}<br>
<br>
If I replace the template argument in for the argument rhs with 3, then<br>
everything works, but that's not really a nice solution :)<br>
<br>
Is it me trying to use templates in a way they aren't meant to be used or is<br>
this a bug in dmd? If it is a bug, does anyone have an idea how to solve<br>
this? I wouldn't mind fixing it in dmd myself, I just need some guidelines.<br>
</blockquote>
<br></div></div>
It is a bug, someone just brought up almost exactly this problem in d.learn.<br>
<br>
<a href="http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D.learn&article_id=28455" target="_blank">http://www.digitalmars.com/<u></u>webnews/newsgroups.php?art_<u></u>group=digitalmars.D.learn&<u></u>article_id=28455</a><br>

<br>
The proper workaround (and actually, the cleaner solution) is to use Vector and not Vector!D.  Inside a template, the name of the template is the same as if you invoked the template with the same template parameters.<br>

<br>
If you actually do need a different value for D, I'm not sure what works.<br>
<br>
-Steve<br>
</blockquote></div><br>Oh nice. I'll go with the clean solution then and disregard the bug for now. Thanks for the quick help.
<div><br></div><div>/asger</div>