Outside array bounds

Flaze07 christianseiji.cs at gmail.com
Sat Jul 7 08:21:08 UTC 2018


On Saturday, 7 July 2018 at 08:09:51 UTC, vino.B wrote:
> Hi All,
>
>   Request you help, on the below code
>
> import std.stdio: writeln;
>
> void process(T ...)(string ID, T args) {
> if (ID == "I1") { writeln(args.length, "\t", args[0]); }
> else if (ID == "I2") { writeln(args.length, "\t", args[1]);}
> }
>
> void main() {
> string S1 = "Test1", S2 = "Test2", ID1 = "I1", ID2 = "I2";
> int Size = 1;
> process(ID1, S1);
> process(ID2, S2, Size);
> }
>
> Error:
> Test.d(5): Error: array index [1] is outside array bounds [0 .. 
> 1]
> Test.d(11): Error: template instance `Test.process!string` 
> error instantiating
>
> From,
> Vino.B

in the first process template instantiation, you have a code that 
has args[ 1 ], despite the size being 1, which means you can only 
have args[ 0 ], how about changing the code to instead args[ $ - 
1 ] ?


More information about the Digitalmars-d-learn mailing list