www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Quick question about new semantics

reply "monarch_dodra" <monarchdodra gmail.com> writes:
I have a struct, which defines a constructor that takes an 
argument.

Now, I'd like to new this object, to it's default T.init value 
(eg call new, but now constructors):

--------
struct S
{
     this(int);
}

void main()
{
     auto p1 = new S;
     auto p2 = new S();
}
--------
main.d(8): Error: constructor main.S.this (int) is not callable 
using argument types ()
main.d(8): Error: expected 1 function arguments, not 0
main.d(9): Error: constructor main.S.this (int) is not callable 
using argument types ()
main.d(9): Error: expected 1 function arguments, not 0
--------
Is this a bug? If "auto a = S();" is legal, how can "auto p = new 
S();" not be?
Sep 14 2012
parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Fri, 14 Sep 2012 14:27:56 -0400, monarch_dodra <monarchdodra gmail.com>  
wrote:

 I have a struct, which defines a constructor that takes an argument.

 Now, I'd like to new this object, to it's default T.init value (eg call  
 new, but now constructors):

 --------
 struct S
 {
      this(int);
 }

 void main()
 {
      auto p1 = new S;
      auto p2 = new S();
 }
 --------
 main.d(8): Error: constructor main.S.this (int) is not callable using  
 argument types ()
 main.d(8): Error: expected 1 function arguments, not 0
 main.d(9): Error: constructor main.S.this (int) is not callable using  
 argument types ()
 main.d(9): Error: expected 1 function arguments, not 0
 --------
 Is this a bug? If "auto a = S();" is legal, how can "auto p = new S();"  
 not be?

It is a bug. http://d.puremagic.com/issues/show_bug.cgi?id=4247 -Steve
Sep 14 2012