jeudi 28 septembre 2017

C++ grouping rules with FOR and IF statements

Just got burned by a strange grouping issue involving a for loop and if statement. I wrote:

for (int i = 0; i < baseMtx->valueSize; i++)
    if (!tempMtx->copyValue(baseMtx, i, i, scaleMult, scaleImag))
        throw ErrorClass("Matrix expansion failed while copying element values");

This throws the error after exiting the loop, and I have not been able figure out why. The way I understand grouping rules in C++, this should be equivalent to

for (int i = 0; i < baseMtx->valueSize; i++)
{
    if (!tempMtx->copyValue(baseMtx, i, i, scaleMult, scaleImag))
        throw ErrorClass("Matrix expansion failed while copying element values");
}

--- but it isn't. For future reference and as a learning experience, can someone explain why the brackets are necessary?

Aucun commentaire:

Enregistrer un commentaire