Boost logo

Boost :

From: David Abrahams (david.abrahams_at_[hidden])
Date: 2001-06-27 12:13:28


----- Original Message -----
From: "John Max Skaller" <skaller_at_[hidden]>
.com wrote:
>
> > I recall a little debate a while back over an alternate implementation:
> > #ifdef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
> > # define BOOST_STATIC_CONSTANT(type, name, value) enum { name = value }
>
> I think this macro is useless and should be removed altogether.
>
> 1. If you're declaring an integral constant, use an enum
>
> 2. Otherwise make the definition out of class.
>
> The only benefit of inclass static const initialisation
> is syntactic, and that benefit is destroyed by using
> a macro. [And I'd argue the enum is terser anyhow]
>
> Have I missed something?

Only that sometimes it's important to carry type information with the static
constant (e.g. in numeric_limits<>). An enum doesn't do that. Also, it is
incapable of holding certain integral values on some platforms (e.g. long
long on MSVC).

I have also found cases with Borland where the enum didn't work but a static
integral constant did work. I think these cases are very rare, though.

Still, when I introduced that macro I was unaware of the language rule
requiring an out-of-class declaration for integral constant members of
templates (unenforced by most compilers). I think I've changed my mind about
the best practice: I would now recommend using an enum whenever you can get
away with it. The out-of-class declaration just makes things so much more
complicated, especially when BOOST_NO_INCLASS_MEMBER_INITIALIZATION
platforms will be using an enum. Then you still need to write

#ifndef BOOST_NO_INCLASS_MEMBER_INITIALIZATION
template <class T> bool const foo<T>::constant;
#endif

Ick.

So I think I'm in favor of converting /most/ uses of BOOST_STATIC_CONSTANT
back to enum declarations at some point.

-Dave


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