Search code examples
c++optimizationcppcheck

cppcheck suggests to reduce scope of variable, should I?


So I have something like this

    int age;

    for (auto person : persons) {
        age = std::stoi(person[0]);

        /* Do other stuff with age in this loop*/
    }

Other than style, are there any performance benefits to declaring int age = std::stoi(person[0]); inside the loop?


Solution

  • are there any performance benefits

    Because of the "as-if" rule, it's reasonable to assume that there is no performance difference between two equivalent ways of declaring the same variable.