www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - equivalent of C++ implicit constructors and conversion operators

reply #ponce <spam spam.spam> writes:
In C++ implicit constructors and conversion operators allow a user-defined type
to act quite like a builtin-type.

  struct half
  {
      half(float x);l
      inline operator float() const;
  }

allows to write:

  half x = 1.f;
  float f = x;

and this is especially useful for templates. 
I couldn't find a similar construct in D, is there any?
Apr 23 2010
parent reply Robert Clipsham <robert octarineparrot.com> writes:
On 23/04/10 17:22, #ponce wrote:
 In C++ implicit constructors and conversion operators allow a user-defined
type to act quite like a builtin-type.

    struct half
    {
        half(float x);l
        inline operator float() const;
    }

 allows to write:

    half x = 1.f;
    float f = x;

 and this is especially useful for templates.
 I couldn't find a similar construct in D, is there any?

A combination of alias this and opAssign should work: ---- struct half { float f; alias f this; half opAssign(float fl) { f = fl; return this; } } void main() { half x; // For some reason I got a compilation error with half x = 1f; x = 1f; float f = x; } ----
Apr 23 2010
parent Philippe Sigaud <philippe.sigaud gmail.com> writes:
--00032555ad522b39650484edb99a
Content-Type: text/plain; charset=ISO-8859-1

On Fri, Apr 23, 2010 at 18:46, Robert Clipsham <robert octarineparrot.com>wrote:

 On 23/04/10 17:22, #ponce wrote:

 In C++ implicit constructors and conversion operators allow a user-defined
 type to act quite like a builtin-type.

   struct half
   {
       half(float x);l
       inline operator float() const;
   }

 allows to write:

   half x = 1.f;
   float f = x;

 and this is especially useful for templates.
 I couldn't find a similar construct in D, is there any?

A combination of alias this and opAssign should work: ---- struct half { float f; alias f this; half opAssign(float fl) { f = fl; return this; } } void main() { half x; // For some reason I got a compilation error with half x = 1f; x = 1f; float f = x; }

Could the compilation error comes from the compiler rewriting half f = 1f; into half f(1f); or somesuch and trying to call a constructor? I'm tearing my hair due to errors/confusions between constructors and opCall for structs. Anyway, in your example, adding this(float f) { this.f = f;} is enough to get half f = 1f; to compile. --00032555ad522b39650484edb99a Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable <div class=3D"gmail_quote">On Fri, Apr 23, 2010 at 18:46, Robert Clipsham <= span dir=3D"ltr">&lt;<a href=3D"mailto:robert octarineparrot.com">robert oc= tarineparrot.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, 2= 04); padding-left: 1ex;"> <div><div></div><div class=3D"h5">On 23/04/10 17:22, #ponce 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;"> In C++ implicit constructors and conversion operators allow a user-defined = type to act quite like a builtin-type.<br> <br> =A0 struct half<br> =A0 {<br> =A0 =A0 =A0 half(float x);l<br> =A0 =A0 =A0 inline operator float() const;<br> =A0 }<br> <br> allows to write:<br> <br> =A0 half x =3D 1.f;<br> =A0 float f =3D x;<br> <br> and this is especially useful for templates.<br> I couldn&#39;t find a similar construct in D, is there any?<br> </blockquote> <br></div></div> A combination of alias this and opAssign should work:<br> ----<br> struct half<br> {<br> =A0float f;<br> =A0alias f this;<br> =A0half opAssign(float fl)<br> =A0{<br> =A0 =A0f =3D fl;<br> =A0 =A0return this;<br> =A0}<br> }<br> <br> void main()<br> {<br> =A0half x;<br> =A0// For some reason I got a compilation error with half x =3D 1f;<br> =A0x =3D 1f;<br> =A0float f =3D x;<br> }<br></blockquote><div><br>Could the compilation error comes from the compi= ler rewriting half f =3D 1f; into half f(1f); or somesuch and trying to cal= l a constructor?<br>I&#39;m tearing my hair due to errors/confusions betwee= n constructors and opCall for structs.<br> <br>Anyway, in your example, adding <br><br>this(float f) { this.f =3D f;}<= br><br>is enough to get half f =3D 1f; to compile.<br><br></div></div> --00032555ad522b39650484edb99a--
Apr 23 2010