digitalmars.D.learn - accessing numeric template parameters
- Dominikus Dittes Scherkl (20/20) Nov 03 2014 If I have a struct with numeric template parameter, how can I
- MrSmith (4/24) Nov 03 2014 You cannot assign to it, because it is only avaliable during
- Philippe Sigaud via Digitalmars-d-learn (19/28) Nov 03 2014 base is part of the type. polynomial is just a 'recipe' for a type,
- Dominikus Dittes Scherkl (6/31) Nov 05 2014 Yes, that's what I intend.
If I have a struct with numeric template parameter, how can I access it within member functions? Like normal member variables? And how about the constructor? struct polynomial(uint base) { private: uint[] N; public: this(uint x) { base = x; } ... void add(Polynomial!base P) { if(N.length < P.N.length) N.length = P.N.length; foreach(i; 0..P.N.length) { N[i] = (N[i]+P.N[i]) % base; } } } This doesn't work for me :-/
Nov 03 2014
On Monday, 3 November 2014 at 14:27:47 UTC, Dominikus Dittes Scherkl wrote:If I have a struct with numeric template parameter, how can I access it within member functions? Like normal member variables? And how about the constructor? struct polynomial(uint base) { private: uint[] N; public: this(uint x) { base = x; } ... void add(Polynomial!base P) { if(N.length < P.N.length) N.length = P.N.length; foreach(i; 0..P.N.length) { N[i] = (N[i]+P.N[i]) % base; } } } This doesn't work for me :-/You cannot assign to it, because it is only avaliable during compilation. Think of it as an immediate value, not variable.
Nov 03 2014
On Mon, Nov 3, 2014 at 3:27 PM, Dominikus Dittes Scherkl via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> wrote:If I have a struct with numeric template parameter, how can I access it within member functions? Like normal member variables? And how about the constructor? struct polynomial(uint base) { private: uint[] N; public: this(uint x) { base = x; }base is part of the type. polynomial is just a 'recipe' for a type, the real struct would be Polynomial!(0), Polynomial!(1), etc. Note that Polynomial!0, Polynomial!1, ... are all different types. Being part of the type means it's defined only at compile-time, you cannot use a runtime value (like 'x') to initialize it. Note that with your current code, `base' is not visible outside Polynomial. You can alias it to a field to make it visible: struct Polynomial(uint base) { alias b = base; // b is visible outside (but set at compile-time !) ... } You can create one like this: Polynomial!2 poly; poly.N = [0,1,0,0,1,1]; assert(poly.b == 2); Of course, you cannot change b: `poly.b = 3;' is forbidden.
Nov 03 2014
On Monday, 3 November 2014 at 21:17:09 UTC, Philippe Sigaud via Digitalmars-d-learn wrote:Yes, that's what I intend.struct polynomial(uint base) { private: uint[] N; public: this(uint x) { base = x; }base is part of the type. polynomial is just a 'recipe' for a type, the real struct would be Polynomial!(0), Polynomial!(1), etc. Note that Polynomial!0, Polynomial!1, ... are all different types.Being part of the type means it's defined only at compile-time, you cannot use a runtime value (like 'x') to initialize it. Note that with your current code, `base' is not visible outside Polynomial. You can alias it to a field to make it visible: struct Polynomial(uint base) { alias b = base; // b is visible outside (but set atAh, ok. Thank you!compile-time !) ... } You can create one like this: Polynomial!2 poly; poly.N = [0,1,0,0,1,1];Ok, now I remember, struct doesn't need an explicit constructor. (in this case)
Nov 05 2014