jhourback
jhourback

Reputation: 4571

Is there a gcc warning flag for when short-circuit evaluation may result in a function not being called?

For example, in the code:

bool foo = bar || baz();

the function baz() may not be called. I've found that this is a common source of bugs for me and I'm wondering if there's a way for GCC to print a warning.

Upvotes: 3

Views: 148

Answers (1)

S.S. Anne
S.S. Anne

Reputation: 15576

There is not a warning flag for this, it would generate a warning for too many common conditions (if(condition) bar();, if(foo && foo->bar) baz();, etc).

Instead, do something similar to this:

bool foo = baz() || bar;

or this:

bool foo = bar | baz();

These unconditionally call baz().

Upvotes: 1

Related Questions