Boost logo

Boost :

From: Ed Brey (brey_at_[hidden])
Date: 2000-09-19 11:37:09


In the random library equal_signed_unsigned and lessthan_signed_unsigned
each cause VC6SP4 to generate an warning that not all control paths return a
value. This is because there is an if/else/if... chain that ends with an
"else if", but no terminating "else", so the compiler can't deduce that that
at least one path will be taken within the if/else/if construct. The
compiler isn't smart enough to realize that the final "if" is actually
unnecessary, and so that path will actually always be taken.

Likewise, an optimizer may not be able to deduce this either, and so for
efficiency sake, as well as to prevent the warning, the final
"if(y_traits::is_signed)" should probably be replaced by a comment.

Note that the warning in question is 4715, which is generally valuable and
shows up in debug and release mode, unlike the "unreachable code" warning
4702 which is generally useless since it is triggered by many forms of
correct code.

The following code reproduces the problem (with or w/o optimizations turned
on):

// Turn off useless warnings
#pragma warning (disable: 4512 4514 4710 4702)

#include <boost/random.hpp>

int main() {
 boost::minstd_rand r;
 boost::random_number_generator<boost::minstd_rand> rg(r);
 return int(rg(10));
}

<wax style="philosophically">
To really make things easy on the compiler, the if/else/if construct can be
eliminated completely in favor of two outer "if"s, since every path within
an "if" statement returns from a function. However, this gets into the
issue of style, pitting the principle of minimizing conditional constructs
versus optically enhancing case breakdown. VC6 is good enough that there is
no need to remove the non-terminating "else if"s, and so the style can
generally be left to the author's choosing.
</wax>


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk