Topic: what is const enough
Author: fjh@munta.cs.mu.OZ.AU (Fergus Henderson)
Date: 1995/07/16 Raw View
spencer@ERA.COM (Spencer Allain) writes:
>Below is a piece of code that simulates including a header file that
>defines the class F, where class F is kept in a library that will
>be linked against by the main() routine code; hence the reason for
>F::constant_int being assigned after the main() routine.
>
>I have used 3 different C++ compilers, and although the messages
>were slightly different as to why this snippet of code won't
>compile, they all agreed that F::constant_int in the case statement
>wasn't a constant.
A "constant expression" has to have a known value at compile time.
For a `const' variable to be a "constant expression", it must have
been initialized in the current scope.
Note that you can initialize an integral static const class member
in the class declaration. (This ability was added to the language
about a year or two ago --- some compilers may not support it yet.)
>------------------------------------------------------------------------------
>class F
>{
>public:
> static int const constant_int ;
Change that to
static int const constant_int = 3;
>} ;
>
>int main()
>{
> int i = 3 ;
>
> switch(i)
> {
> case F::constant_int: // not const enough for compilers
> break ;
> default:
> break ;
> }
>
> return(0) ;
>}
>
>int const F::constant_int = 3 ;
Change that to
int const F::constant_int;
>------------------------------------------------------------------------------
>
>Granted, I have gotten around the problem by placing my "constant" value
>inside of an enum, but I'm wondering what the working paper specifically
>says about this situation.
I think the reason the committee decided to allow in-class initialization
for integeral static const members was that they didn't like the enum
hack you mentioned ;-)
--
Fergus Henderson
fjh@cs.mu.oz.au
http://www.cs.mu.oz.au/~fjh
PGP key fingerprint: 00 D7 A2 27 65 09 B6 AC 8B 3E 0F 01 E7 5D C4 3F
Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 1995/07/06 Raw View
In article 95Jul5101616@zorgon.ERA.COM, spencer@ERA.COM (Spencer Allain) writes:
>
>I have used 3 different C++ compilers, and although the messages
>were slightly different as to why this snippet of code won't
>compile, they all agreed that F::constant_int in the case statement
>wasn't a constant.
>
>class F
>{
>public:
> static int const constant_int ;
>} ;
>
>int main()
>{
> int i = 3 ;
>
> switch(i)
> {
> case F::constant_int: // not const enough for compilers
> break ;
> default:
> break ;
> }
>
> return(0) ;
>}
>
>int const F::constant_int = 3 ;
>
>Granted, I have gotten around the problem by placing my "constant" value
>inside of an enum, but I'm wondering what the working paper specifically
>says about this situation.
The draft standard is no different from the ARM for this example.
The term "constant" is a bit fuzzy. C++ has different kinds of "constant".
A "constant-expression" has a rather complicated definition which I won't
repeat here, but you can think of it loosely as an expression whose value
is known at compile time. (It takes a full page in the standard to
define fully.)
A case-label must be a constant-expression.
"const" applied to an object means that you are not allowed to assign
to the object. By itself, "const" doesn't mean the value of the object
may be used in a constant-expression.
A const object of simple type which has a visible initializer may be used
whenever a constant-expression of that type is needed.
The draft standard introduces one more kind of constant: A static data
member of a class of integral or enum type may be initialized in the
class definition, in which case its value is a constant-expression.
You still need to define the data member outside the class, but must
omit the initializer in that definition.
Your example does not provide a visible initializer for F::constant_int at
the point where it is used, and so it is not a constant-expression.
---
Steve Clamage, stephen.clamage@eng.sun.com
Author: spencer@ERA.COM (Spencer Allain)
Date: 1995/07/05 Raw View
Below is a piece of code that simulates including a header file that
defines the class F, where class F is kept in a library that will
be linked against by the main() routine code; hence the reason for
F::constant_int being assigned after the main() routine.
I have used 3 different C++ compilers, and although the messages
were slightly different as to why this snippet of code won't
compile, they all agreed that F::constant_int in the case statement
wasn't a constant.
------------------------------------------------------------------------------
class F
{
public:
static int const constant_int ;
} ;
int main()
{
int i = 3 ;
switch(i)
{
case F::constant_int: // not const enough for compilers
break ;
default:
break ;
}
return(0) ;
}
int const F::constant_int = 3 ;
------------------------------------------------------------------------------
Granted, I have gotten around the problem by placing my "constant" value
inside of an enum, but I'm wondering what the working paper specifically
says about this situation.
----------------------------------------------------------------------
Spencer Allain E-mail: spencer@era.com
Engineering Research Associates Phone : (703) 734-8800 x1414
1595 Spring Hill Road Fax : (703) 827-9411
Vienna, VA 22182-2235
----------------------------------------------------------------------