www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Small opCall problem

reply bearophile <bearophileHUGS lycos.com> writes:
This is a small C++ program that compiles:

template<typename T> struct Foo {
    Foo(T x) {}
    template<typename U> void operator()(U y) {}
};
int main() {
    Foo<int> foo(1);
    foo(1.5);
}


I think this is an equivalent D2 program:

struct Foo(T) {
    this(T x) {}
    void opCall(U)(U y) {}
}
void main() {
    auto foo = Foo!int(1);
    foo(1.5);
}


But dmd 2.046 prints:
temp.d(7): Error: constructor temp.Foo!(int).Foo.this (int x) is not callable
using argument types (double)
temp.d(7): Error: cannot implicitly convert expression (1.5) of type double to
int

Is this a bug in my D code? If it's a bug in my D code, do you know how to
translate that C++ code in D2?

Bye and thank you,
bearophile
May 30 2010
next sibling parent Philippe Sigaud <philippe.sigaud gmail.com> writes:
--0016e65b3ffcf1e8d30487d22100
Content-Type: text/plain; charset=ISO-8859-1

On Sun, May 30, 2010 at 17:04, bearophile <bearophileHUGS lycos.com> wrote:

 struct Foo(T) {
    this(T x) {}
    void opCall(U)(U y) {}
 }
 void main() {
    auto foo = Foo!int(1);
    foo(1.5);
 }

I've had this one too. I think it's a bug, because foo is already constructed when foo(1.5) is used. So the compiler should know it's an opCall and not a constructor call. The only solution I found was a kludge: struct Foo(T) { void initialize(T)(T x) {} // in my case, their was some data initialization there. void opCall(U)(U y) {} } Foo!T foo(T)() { Foo!T f; f.initialize(); return f;} void main() { auto f = foo(1); f(1.5); } Philippe --0016e65b3ffcf1e8d30487d22100 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable <br><br><div class=3D"gmail_quote">On Sun, May 30, 2010 at 17:04, bearophil= e <span dir=3D"ltr">&lt;<a href=3D"mailto:bearophileHUGS lycos.com">bearoph= ileHUGS lycos.com</a>&gt;</span> wrote:<br><blockquote class=3D"gmail_quote= " style=3D"margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, = 204); padding-left: 1ex;"> <div id=3D":jy" class=3D"ii gt">struct Foo(T) {<br> =A0 =A0this(T x) {}<br> =A0 =A0void opCall(U)(U y) {}<br> }<br> void main() {<br> =A0 =A0auto foo =3D Foo!int(1);<br> =A0 =A0foo(1.5);<br> }</div></blockquote></div><br>I&#39;ve had this one too. I think it&#39;s a= bug, because foo is already constructed when foo(1.5) is used. So the comp= iler should know it&#39;s an opCall and not a constructor call.<br>The only= solution I found was a kludge:<br> <br><br>struct Foo(T)<br>{<br>=A0=A0 void initialize(T)(T x) {} // in my ca= se, their was some data initialization there.<br>=A0=A0 void opCall(U)(U y)= {}<br>}<br><br>Foo!T foo(T)() { Foo!T f; f.initialize(); return f;}<br><br=
void main() {<br>

<br> --0016e65b3ffcf1e8d30487d22100--
May 30 2010
prev sibling next sibling parent reply BCS <none anon.com> writes:
Hello bearophile,

 struct Foo(T) {
 this(T x) {}
 void opCall(U)(U y) {}
 }
 void main() {
 auto foo = Foo!int(1);
 foo(1.5);
 }

FWIW, The struct being a template is extraneous.
 temp.d(7): Error: constructor temp.Foo!(int).Foo.this (int x) is not callable 

The lookup seems to think you are calling the constructor. Should make it easy to find the bug. (And yes, I think this is a bug) -- ... <IXOYE><
May 30 2010
parent bearophile <bearophileHUGS lycos.com> writes:
BCS:
 FWIW, The struct being a template is extraneous.

You are right, thank you. I have added the simplified example: http://d.puremagic.com/issues/show_bug.cgi?id=4253 Bye, bearophile
May 30 2010
prev sibling parent Philippe Sigaud <philippe.sigaud gmail.com> writes:
--0016e65b60f28edb870487d28b37
Content-Type: text/plain; charset=ISO-8859-1

On Sun, May 30, 2010 at 18:31, bearophile <bearophileHUGS lycos.com> wrote:

 BCS:
 FWIW, The struct being a template is extraneous.

You are right, thank you. I have added the simplified example: http://d.puremagic.com/issues/show_bug.cgi?id=4253

Philippe --0016e65b60f28edb870487d28b37 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable <br><br><div class=3D"gmail_quote">On Sun, May 30, 2010 at 18:31, bearophil= e <span dir=3D"ltr">&lt;<a href=3D"mailto:bearophileHUGS lycos.com">bearoph= ileHUGS lycos.com</a>&gt;</span> wrote:<br><blockquote class=3D"gmail_quote= " style=3D"margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, = 204); padding-left: 1ex;"> BCS:<br> <div class=3D"im">&gt; FWIW, The struct being a template is extraneous.<br> <br> </div>You are right, thank you. I have added the simplified example:<br> <a href=3D"http://d.puremagic.com/issues/show_bug.cgi?id=3D4253" target=3D"= _blank">http://d.puremagic.com/issues/show_bug.cgi?id=3D4253</a><br> <br></blockquote><div><br>This one had me gnashing my teeth. I voted it up.= <br><br><br>Philippe=A0</div></div> --0016e65b60f28edb870487d28b37--
May 30 2010