CTFE question

Philippe Sigaud philippe.sigaud at gmail.com
Mon Sep 3 06:47:12 PDT 2012


On Mon, Sep 3, 2012 at 1:27 PM, Don Clugston <dac at nospam.com> wrote:


>> Godd adivce, except beware of using ++ and --, they don't work at
>> compile-time. I'm regularly caught unaware by this, particularly while
>> looping.
>
>
> Really? That's scary. Is there a bug report for this?

I re-tried this and I was mistaken: it's not ++ or --, it's changing
an iteration index at compile-time. My bad.

int[] indices(string s)
{
    int[] store;
    foreach(i,ch; s[0..$-1])
    {
        store ~= i;
        if (s[i..i+2] == "aa")
            i += 1; // also ++i or i++, no difference here
    }
    return store;
}

void main()
{
    enum result1 = indices("aaabc");
    auto result2 = indices("aaabc");

    writeln(result1); // [0,1,2,3]
    writeln(result2); // [0,2,3]
}

I know changing 'i' while iterating is not a good idea, but at least
in this case, CT-executed and RT-execution give a different result.
All my other tests with ++ and -- give correct results, sorry for the
bad publicity.

Worth a bug report?


More information about the Digitalmars-d-learn mailing list