Boost logo

Boost :

From: Terje Slettebø (tslettebo_at_[hidden])
Date: 2003-01-07 19:23:05


>From: "Gennaro Prota" <gennaro_prota_at_[hidden]>

> On Tue, 7 Jan 2003 15:35:56 +0100, Terje Slettebø
> <tslettebo_at_[hidden]> wrote:
>
> [...]
> >I like static const, as I think it conveys more succinctly what it's
about,
> >and the enum appears more like a hack (for compilers not handling static
> >const in-class initialisation). However, if this means it may need an
> >out-of-class definition, as well, perhaps this could need to be
> >reconsidered?
>
> It *may* need out-of-class definition, as you say.

Actually, it's pretty clear that in this case, the out-of-class definition
is required. Intel C++ 7.0, running in strict mode, certainly needs it. It
gives a link-error without it, for the program I gave. GCC 3.2 gives a
link-error, as well.

> This could also be a boost FAQ ;-)
> http://lists.boost.org/MailArchives/boost/msg35797.php

I am well aware of the DR that allows static const in-class initialisation
to omit having an external definition (I read that DR item, earlier _today_,
:) as I was browsing through the list), and I've also read that posting,
earlier, so this was nothing new to me. Don't you think I know this? :)

However, what is new here, is that the example program took the integral
constant by const reference ("using the object", in standardese), which
meant that it required the definition outside the class, as well.

This is not the case if you use enum.

DR 48 says:

"The member shall still be defined in a namespace scope if it is used in the
program in the manner described in 3.2 basic.def.odr . The namespace scope
definition shall not contain an initializer."

3.2 (DR version): "An expression is potentially evaluated unless it appears
where an integral constant expression is required (see 5.19 expr.const ),
is the operand of the sizeof operator (5.3.3 expr.sizeof ), or is the
operand of the typeid operator and the expression does not designate an
lvalue of polymorphic class type (5.2.8 expr.typeid ). An object or
non-overloaded function is used if its name appears in a
potentially-evaluated expression."

5.19: "Constant expressions
[...] An integral constant-expression can involve [...] In particular,
except in sizeof expressions, functions, class objects, pointers, or
*references* shall not be used, and assignment, increment, decrement,
function-call, or comma operators shall not be used." (My emphasis)

> As to deprecating BOOST_STATIC_CONSTANT, which David B. Held proposed,
> I don't think it is a good idea. There is code that works well either
> with static const and with the enum in its 'normal' use, but fails
> miserably when used in other contexts where the type of the constant
> matters. BOOST_STATIC_CONSTANT allows at least the most common uses
> with broken compilers.

It is true that there are issues with the type of the constant, so that it
may select the base template or a wrong specialisation, if it expects bool,
but gets an enum. So maybe it's best after all, to leave it as it is.

Regards,

Terje


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