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

Interesting.. Looks like the compiler does some boundschecking during compile time. You could circumvent this:


    void process(T ...)(string ID, T args) {
        if (ID == "I1") { writeln(args.length, "\t", args[0]); }
static if (T.length > 1) // only add below code if cond. true if (ID == "I2") { writeln(args.length, "\t", args[1]);}
    }

Oddly, when leaving out the "static if" statement, the error still occurs when compiling with "-release" or with "-boundscheck=off"... I thought those would/should disable bounds checking at compile time???

Reply via email to