accessing numeric template parameters

Dominikus Dittes Scherkl via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Nov 5 12:03:26 PST 2014


On Monday, 3 November 2014 at 21:17:09 UTC, Philippe Sigaud via
Digitalmars-d-learn wrote:
>> 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.
Yes, that's what I intend.
>
> 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

Ah, 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)


More information about the Digitalmars-d-learn mailing list