digitalmars.D.learn - Default constructor for structs
- Peter Alexander (10/10) Aug 07 2010 Since default constructors for structs are not allowed, how do I go
- bearophile (43/46) Aug 07 2010 This doesn't work, I don't know why:
- Peter Alexander (11/15) Aug 07 2010 Thanks for your detailed reply.
- rouge (2/15) Aug 07 2010
- rouge (4/20) Aug 07 2010 oops :p correction:
Since default constructors for structs are not allowed, how do I go about making all the elements of this Matrix struct default to zero? (floats default to nan by default). struct Matrix(int M, int N) { float[M][N] elements; } Matrix(3, 3) m; assert(m[0][0] == 0); ?
Aug 07 2010
Peter Alexander:Since default constructors for structs are not allowed, how do I go about making all the elements of this Matrix struct default to zero? (floats default to nan by default).This doesn't work, I don't know why: float[M][N] elements = 0.0; A bad looking solution: struct Matrix(int M, int N) { float[M][N] elements; static Matrix opCall() { Matrix m; foreach (ref row; m.elements) row[] = 0.0; return m; } } void main() { auto m = Matrix!(3, 3)(); assert(m.elements[0][0] == 0); } A possible solution avoids going against the language (the default nan value for FP values isn't there for show, it's there because it improves your code): struct Matrix(int M, int N) { float[M][N] elements; this(float init=0) { foreach (ref row; elements) row[] = init; } } void main() { auto m = Matrix!(3, 3)(0.0); assert(m.elements[0][0] == 0); } This doesn't work: struct Matrix(int M, int N) { float[M][N] elements; this(float init=0.0) { foreach (ref row; elements) row[] = init; } } void main() { auto m = Matrix!(3, 3)(); assert(m.elements[0][0] == 0); } Bye, bearophile
Aug 07 2010
On 7/08/10 3:44 PM, bearophile wrote:Peter Alexander: <snip> Bye, bearophileThanks for your detailed reply. However, I'm not particularly satisfied with any of those :( I have already gone with the single parameter init constructor in my code until I find something better, but really, I just want the matrix to be default initialised to zeroes without having to explicitly specify it in client code. Initialising to NaNs is highly unintuitive (especially since ints auto-initialise to 0...) and this has already caused me unnecessary grief. I understand the reasoning, and I might agree with it if it wasn't forced on you...
Aug 07 2010
Hi, I'm a newbie, hope the attached example works for you. Peter Alexander wrote:Since default constructors for structs are not allowed, how do I go about making all the elements of this Matrix struct default to zero? (floats default to nan by default). struct Matrix(int M, int N) { float[M][N] elements; } Matrix(3, 3) m; assert(m[0][0] == 0); ?
Aug 07 2010
oops :p correction: in template Zero const float[M][N] ==> const T[M][N] rouge wrote:Hi, I'm a newbie, hope the attached example works for you. Peter Alexander wrote:Since default constructors for structs are not allowed, how do I go about making all the elements of this Matrix struct default to zero? (floats default to nan by default). struct Matrix(int M, int N) { float[M][N] elements; } Matrix(3, 3) m; assert(m[0][0] == 0); ?
Aug 07 2010