I have a class, which among other things stores vectors of a few other objects I have defined. I want to define a test()
function which checks to see if all the objects are in a valid state and then call this via assert
so that it only runs in debug mode and outputs each error that is found. However, only the first error is outputted.
Consider the following toy example:
class Foo
{
public:
Foo(int index, int data) : _index(index), _some_data(data) {}
bool test() const
{
bool valid = true;
if (_some_data == 0)
{
std::cout << "Error! Foo " << _index << " has invalid data.\n";
valid = false;
}
return valid;
}
private:
int _index;
int _some_data; // for this example, should be non-zero
};
class MyClass
{
public:
MyClass(std::vector<Foo> foos) : _foos(foos) {}
bool test() const
{
bool valid = true;
for (auto &foo : _foos)
{
valid = valid && foo.test();
}
return valid;
}
private:
std::vector<Foo> _foos;
};
int main()
{
Foo foo1(0, 0);
Foo foo2(1, 0);
Foo foo3(2, 0);
std::vector<Foo> foos({foo1, foo2, foo3}); // all three foos in invalid state
MyClass my_class(foos);
assert(my_class.test()); // outputs "Error! Foo 0 has invalid data." But does not show me errors in other two foos
}
Why does this output only the first invalid Foo and not every invalid Foo?
I guess this has something to do with the compiler trying to optimise the code (I'm running GCC). As soon as valid
is equal to false
it does not bother running the rest of the loop. How do I prevent this?