www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - no-argument constructor: is this a bug?

reply Caligo <iteronvexor gmail.com> writes:
struct A(uint samples){

  float[samples] _data = void;

  this(float val = 0.0f){ fill(_data[], val); }
}


  auto a = A!8();

a._data is filled with garbage instead of zeros because the
no-argument constructor is called instead of the one that I've
defined.
Jan 22 2012
parent Timon Gehr <timon.gehr gmx.ch> writes:
On 01/23/2012 12:51 AM, Caligo wrote:
 struct A(uint samples){

    float[samples] _data = void;

    this(float val = 0.0f){ fill(_data[], val); }
 }


    auto a = A!8();

 a._data is filled with garbage instead of zeros because the
 no-argument constructor is called instead of the one that I've
 defined.

structs are always default-constructible, and, as a tie-breaker, a function definition that has the exact number of arguments is considered a better match one that has to supply default-arguments to match. You could use a static opCall to make auto a = A!8() work.
Jan 22 2012