|
Boost : |
From: Terje Slettebø (tslettebo_at_[hidden])
Date: 2003-01-11 21:34:58
>From: "Gabriel Dos Reis" <gdr_at_[hidden]>
> What I find confusing about the current rule is that it makes an
> exception for integral type const static data member. I think the old
> rule was less irregular.
>
> (Strangely enought, for long time -- since the publication of TC++PL3
> in 1997 -- I've been convinced of having read Bjarne write something
> like "it is something I call a misfeature"; but now I'm perfectly
> unable to locate something similar in TC++PL3 "special edition", so
> either I dreamt or that text was removed. I can't tell. Probably the
> former).
No, I don't think you've dreamt it, because I've heard it, too. :) I think
it's in D&E, but I haven't found it there, now.
I think the context of the remark was that the rule makes integral types
special, in that they may be initialised in-class (and not requiring an
out-of-class definition), whereas you can't do the same with e.g. floating
point values, or UDTs.
However, searching for this on Google, I found this quote
(http://www.cs.washington.edu/homes/gjb/doc/pcp3-quals/pcp3-paper.ps.gz):
"Because of the "misfeature" of such declarations [static const] still
requiring definitions outside of the class, Stroustrup still recommends
using enums for static integral constant members (i.e., the "enum hack")
[Str97, p. 249]."
Maybe the "misfeature" was the fact that they may need definition, anyway?
And not the fact that they may also _not_ need definition.
In TC++PL, third edition, p. 249, he talks about use of static const vs
enum, but presents them mostly as alternatives, and without saying anything
about "misfeature".
Trying to find the place of the quote in D&E, I found another interesting
part (p. 90):
--- Start quote ---
In the meantime, I had experimented further with const in C with Classes and
found that const was a useful alternative to macros for representing
constants only if global consts were implicitly local to their compilation
unit. Only in that case could the compiler easily deduce that their value
really didn't change. Knowing that allows us to use simple consts in
constant expressions and to *avoid allocating space for such constants*. [My
emphasis] C did not adopt this rule. For example, in C++ we can write:
const int max = 14;
void f(int i)
{
int a[max+1];
switch(i) {
case max:
// ...
}
whereas in C, even today, we must write
#define max 14
// ...
because in C, consts may not be used in constant expressions. This makes
consts far less useful in C than in C++ and leaves C dependent on the
preprocessor while C++ programmers can use properly typed and scoped consts.
--- End quote ---
This is global const, not class static const, but it shows his appreciation
over that they can be used in integral constant expressions.
Regards,
Terje
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk