digitalmars.D.learn - No constructor for a templated class?
- Philippe Sigaud <philippe.sigaud gmail.com> Jul 25 2010
- div0 <div0 sourceforge.net> Jul 25 2010
- Jacob Carlborg <doob me.com> Jul 26 2010
- Philippe Sigaud <philippe.sigaud gmail.com> Jul 26 2010
- Philippe Sigaud <philippe.sigaud gmail.com> Jul 25 2010
- Mafi <mafi example.org> Jul 25 2010
- Philippe Sigaud <philippe.sigaud gmail.com> Jul 25 2010
OK, I must be tired, I don't know.
While switching from a template struct to a templated class, I got the
following problem:
class A(T)
{
T _t;
this(U)(U u) if (is(U == T))
{
_t = u;
}
}
void main()
{
auto aa = new A!(double)(1.0); // line 12
}
Error (12): no constructor for A.
What am I doing wrong?
Philippe
Jul 25 2010
On 25/07/2010 13:55, Philippe Sigaud wrote:OK, I must be tired, I don't know.
Nope you're not tired. Templated constructor functions are not currently supported in D. :(
Jul 25 2010
On 2010-07-25 23:30, Philippe Sigaud wrote:On Sun, Jul 25, 2010 at 16:08, div0 <div0 sourceforge.net <mailto:div0 sourceforge.net>> wrote: On 25/07/2010 13:55, Philippe Sigaud wrote: OK, I must be tired, I don't know. Nope you're not tired. Templated constructor functions are not currently supported in D. :( Ouch. I did that many many times, but for structs. Case in point, I encountered this while deciding to switch from struct to classes, thinking inheritance would suppress lot of code duplication. Aw, man, first time in months I use classes in D, and it's killed in the womb. OK, there are solutions for for what I envision, but that will be more painful. Thanks, div0
Perhaps a factory function like opCall or something similar ? class Foo { int x; static Foo opCall (T) (T y) { Foo foo = new Foo; foo.x = y; return foo; } } auto foo = Foo(3); -- /Jacob Carlborg
Jul 26 2010
--001485f91cbe261921048c49f440 Content-Type: text/plain; charset=ISO-8859-1 On Mon, Jul 26, 2010 at 13:15, Jacob Carlborg <doob me.com> wrote:Perhaps a factory function like opCall or something similar ? class Foo { int x; static Foo opCall (T) (T y) { Foo foo = new Foo; foo.x = y; return foo; } } auto foo = Foo(3);
--001485f91cbe261921048c49f440 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable <div class=3D"gmail_quote">On Mon, Jul 26, 2010 at 13:15, Jacob Carlborg <s= pan dir=3D"ltr"><<a href=3D"mailto:doob me.com">doob me.com</a>></spa= n> 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;"> Perhaps a factory function like opCall or something similar ?<br> <br> class Foo<br> {<br> =A0 =A0int x;<br> <br> =A0 =A0static Foo opCall (T) (T y)<br> =A0 =A0{<br> =A0 =A0 =A0 =A0Foo foo =3D new Foo;<br> =A0 =A0 =A0 =A0foo.x =3D y;<br> =A0 =A0 =A0 =A0return foo;<br> =A0 =A0}<br> }<br> <br> auto foo =3D Foo(3);<br> <br></blockquote><div><br>Ah yes, I forgot about static opCall. Thanks Jaco= b, I'll try this. <br><br>=A0<br></div></div><br> --001485f91cbe261921048c49f440--
Jul 26 2010
--0016e6d7effb6ff8ed048c3cfae0 Content-Type: text/plain; charset=ISO-8859-1 On Sun, Jul 25, 2010 at 16:08, div0 <div0 sourceforge.net> wrote:On 25/07/2010 13:55, Philippe Sigaud wrote:OK, I must be tired, I don't know.
Nope you're not tired. Templated constructor functions are not currently supported in D. :(
Ouch. I did that many many times, but for structs. Case in point, I encountered this while deciding to switch from struct to classes, thinking inheritance would suppress lot of code duplication. Aw, man, first time in months I use classes in D, and it's killed in the womb. OK, there are solutions for for what I envision, but that will be more painful. Thanks, div0 --0016e6d7effb6ff8ed048c3cfae0 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable <br><br><div class=3D"gmail_quote">On Sun, Jul 25, 2010 at 16:08, div0 <spa= n dir=3D"ltr"><<a href=3D"mailto:div0 sourceforge.net">div0 sourceforge.= net</a>></span> wrote:<br><blockquote class=3D"gmail_quote" style=3D"mar= gin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-= left: 1ex;"> <div class=3D"im">On 25/07/2010 13:55, Philippe Sigaud wrote:<br> <blockquote class=3D"gmail_quote" style=3D"margin: 0pt 0pt 0pt 0.8ex; borde= r-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"> OK, I must be tired, I don't know.<br> </blockquote> <br></div> Nope you're not tired.<br> Templated constructor functions are not currently supported in D. :(<br> </blockquote></div><br>Ouch.<br><br>I did that many many times, but for str= ucts. Case in point, I encountered this while deciding to switch from struc= t to classes, thinking inheritance would suppress lot of code duplication.= =A0 Aw, man, first time in months I use classes in D, and it's killed i= n the womb.<br> OK, there are solutions for for what I envision, but that will be more pain= ful. <br><br>Thanks, div0<br> --0016e6d7effb6ff8ed048c3cfae0--
Jul 25 2010
Am 25.07.2010 14:55, schrieb Philippe Sigaud:OK, I must be tired, I don't know. While switching from a template struct to a templated class, I got the following problem: class A(T) { T _t; this(U)(U u) if (is(U == T)) { _t = u; } }
Probably this case is reduced, but anyways: Having a template(U) where U must be == T (another template parameter) is complettly useless. Maybe I'm missing something but isn't this equivalent to the above: class A(T) { T _t; this(T u) { _t = u; } }
Jul 25 2010
--0016e6d7effb263d7f048c3e0a55 Content-Type: text/plain; charset=ISO-8859-1 On Mon, Jul 26, 2010 at 00:00, Mafi <mafi example.org> wrote:Am 25.07.2010 14:55, schrieb Philippe Sigaud: OK, I must be tired, I don't know.While switching from a template struct to a templated class, I got the following problem: class A(T) { T _t; this(U)(U u) if (is(U == T)) { _t = u; } }
Probably this case is reduced, but anyways: Having a template(U) where U must be == T (another template parameter) is complettly useless. Maybe I'm missing something but isn't this equivalent to the above: class A(T) { T _t; this(T u) { _t = u; } }
most complicated template constraint I've ever build. It's five lines long and invoke somthing like four other templates. It worked for struct A. That's switching to class A that killed everything :-( I've a templated Graph struct, templated on Node and Edge (which may very well be templated types themselves, as the user wish). A very handy constructor, invoked by a factory function, is one taking any number of nodes and edges, checking their compatibility, deducing common properties at compile-time and then constructing the graph. Philippe --0016e6d7effb263d7f048c3e0a55 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable <div class=3D"gmail_quote">On Mon, Jul 26, 2010 at 00:00, Mafi <span dir=3D= "ltr"><<a href=3D"mailto:mafi example.org">mafi example.org</a>></spa= n> 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;"> Am 25.07.2010 14:55, schrieb Philippe Sigaud:<div class=3D"im"><br> <blockquote class=3D"gmail_quote" style=3D"margin: 0pt 0pt 0pt 0.8ex; borde= r-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"> OK, I must be tired, I don't know.<br> While switching from a template struct to a templated class, I got the<br> following problem:<br> <br> class A(T)<br> {<br> =A0 =A0 T _t;<br> =A0 =A0 this(U)(U u) if (is(U =3D=3D T))<br> =A0 =A0 {<br> =A0 =A0 =A0 =A0 _t =3D u;<br> =A0 =A0 }<br> }<br> </blockquote> <br></div> Probably this case is reduced, but anyways:<br> Having a template(U) where U must be =3D=3D T (another template parameter) = is complettly useless. Maybe I'm missing something but isn't this e= quivalent to the above:<div class=3D"im"><br> class A(T)<br> =A0{<br> =A0 =A0 =A0T _t;<br></div> =A0 =A0 =A0this(T u)<br> =A0 =A0 =A0{<br> =A0 =A0 =A0 =A0 =A0_t =3D u;<br> =A0 =A0 =A0}<br> =A0}<br> <br> </blockquote></div><br>Yes, to original case is much more complicated. In f= act, it may well be the most complicated template constraint I've ever = build. It's five lines long and invoke somthing like four other templat= es.<br> <br>It worked for struct A. That's switching to class A that killed eve= rything :-(<br><br>I've a templated Graph struct, templated on Node and= Edge (which may very well be templated types themselves, as the user wish)= . A very handy constructor, invoked by a factory function,=A0 is one taking= any number of nodes and edges, checking their compatibility, deducing comm= on properties at compile-time and then constructing the graph.<br> <br>Philippe<br><br> --0016e6d7effb263d7f048c3e0a55--
Jul 25 2010









Jacob Carlborg <doob me.com> 