www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - No constructor for a templated class?

reply Philippe Sigaud <philippe.sigaud gmail.com> writes:
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
next sibling parent reply div0 <div0 sourceforge.net> writes:
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
parent reply Philippe Sigaud <philippe.sigaud gmail.com> writes:
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
Jul 25 2010
parent reply Jacob Carlborg <doob me.com> writes:
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
parent Philippe Sigaud <philippe.sigaud gmail.com> writes:
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);
Ah yes, I forgot about static opCall. Thanks Jacob, I'll try this.
Jul 26 2010
prev sibling parent reply Mafi <mafi example.org> writes:
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
parent Philippe Sigaud <philippe.sigaud gmail.com> writes:
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; } }
Yes, to original case is much more complicated. In fact, it may well be the 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
Jul 25 2010