No constructor for a templated class?

Jacob Carlborg doob at me.com
Mon Jul 26 04:15:01 PDT 2010


On 2010-07-25 23:30, Philippe Sigaud wrote:
>
>
> On Sun, Jul 25, 2010 at 16:08, div0 <div0 at sourceforge.net
> <mailto:div0 at 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


More information about the Digitalmars-d-learn mailing list