Template class with dispatched properties

Chris Cain clcain at uncg.edu
Thu Nov 7 20:32:27 PST 2013


On Friday, 8 November 2013 at 04:28:31 UTC, Ross Hays wrote:
>
> class Vector(int N, T) if (N <= 3) {
>     T[N] data;
>
>     this()
>     {
>     	data[] = 0;
>     }
>
>     @property ref T opDispatch(string fieldName, Args ...)(Args 
> args)
>         if (Args.length < 2 && fieldName.length == 1 && 
> toOffset(fieldName) < N)
>     {
> 	    int offset = fieldName[0 .. 1] - 'x';
> 	    if (args.length != 0)
> 	    	return data[offset];
> 	    else
> 	    	return data[offset] = args[0];
>     }
> }
>
> Same error.

Sorry, I forgot to mention in that post that you have "toOffset" 
in your template constraint, which means it will also never 
match. You'll have to define it or replace it with `fieldName[0] 
- 'x';`

Also, you might not want to do `fieldName[0 .. 1]` because that's 
a slice (which is just another array of length 1). It won't do 
what you're expecting.


More information about the Digitalmars-d-learn mailing list