|
Boost : |
From: Ed Brey (brey_at_[hidden])
Date: 1999-11-08 09:58:55
[Discussion on working around compiler(s) that don't support constant
variable definitions within the class definition. Two workarounds
were mentioned: the enum hack and defining the constant outside the
class.]
I don't want to get into a long discussion on this issue, since it is
small compared to the big picture of the random_number interface.
However, I ran a quick test is VC6, and found that when a constant is
defined outside the class, the compiler does not generate
immediate-mode instructions and does not perform constant folding.
The way that random number constants tend to be used, I think that
this could be a significant performance problem.
I'd prefer a solution like:
#if !defined(BOOST_NO_IN_CLASS_CONSTANTS)
static const randnum_t rand_max = 0xffffffff;
#else
enum {rand_max = 0xffffffff};
#endif
This provides optimal performance and supports the boost principle of
not kowtowing to broken compilers. Hopefully, the VC bug will be
fixed before the constant outgrows the enum size. If not, another
workaround would be to use a constant that is not a member of the
class at all (again, just for broken compilers); however, I doubt this
will be necessary.
As always, a better name than BOOST_NO_IN_CLASS_CONSTANTS may be
appropriate. Is there another term by which constants defined in the
class are known? "inline constant"?
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk