Thread overview
accessing numeric template parameters
Nov 03, 2014
MrSmith
Nov 03, 2014
Philippe Sigaud
November 03, 2014
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 :-/
November 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.
November 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.
November 05, 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)