Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How is "static const int" better than "static enum"?

Reply
Thread Tools

How is "static const int" better than "static enum"?

 
 
Ajax Chelsea
Guest
Posts: n/a
 
      12-03-2003
can not the "static const int" be replaced by "static enum" anywhere?

is it necessary that define special initialization syntax for "static const int"?
 
Reply With Quote
 
 
 
 
Phlip
Guest
Posts: n/a
 
      12-03-2003
Ajax Chelsea wrote:

> can not the "static const int" be replaced by "static enum" anywhere?


enum is a type, not a variable, so it needs no 'static' storage category.

'int' has an implementation-defined size, and its type is compatible with
variable ints.

'enum' is only guaranteed to have enough bits to store any value used in
their definition.

For a while, compilers could not treat 'static const int' inside a class as
a compile-time constant, and so one couldn't size arrays with it and such.
Using 'enum' as a scalar instead of a typed flag was an easy work-around.

> is it necessary that define special initialization syntax for "static

const int"?

?

Constant static data are the only things that can declare inside a class.
This (I suspect) grants them their compile-time constant status.

This is all well-formed, with defined behavior:

class yo { public:
static int z (42);
};

char whatever[yo::z];

But an enum would have worked the same, too.

--
Phlip


 
Reply With Quote
 
 
 
 
Rob Williscroft
Guest
Posts: n/a
 
      12-03-2003
Phlip wrote in news:eyfzb.33503$. com:

> This is all well-formed, with defined behavior:
>
> class yo { public:
> static int z (42);
> };
>
> char whatever[yo::z];
>
> But an enum would have worked the same, too.
>
>


Also if you also want to use the static integral constant in a
non-compile-time context you also need a definition outside the class,

/* Not in a header file (templates aside)
*/
int yo::z; /* Note no initializer */

int main()
{
int z = yo::z;
int const *zp = &yo:;
}

enum's don't have this requirment, which is perhapse one way in which
enum's are "better" than static int const's.

Cranking the level of triviality up a notch. An instance of an enum
can also be a static integral constant,

#include <iostream>

struct A
{
enum B { C, D, E };
static B const b = A::E;
};

A::B const A::b;

int main()
{
std::cerr << A::b << "\n";
}

Rob.
--
http://www.victim-prime.dsl.pipex.com/
 
Reply With Quote
 
Ajax Chelsea
Guest
Posts: n/a
 
      12-05-2003
Rob Williscroft <> wrote in message news:<Xns9446775E0B83CukcoREMOVEfreenetrtw@195.129 .110.204>...
>
> Cranking the level of triviality up a notch. An instance of an enum
> can also be a static integral constant,
>
> #include <iostream>
>
> struct A
> {
> enum B { C, D, E };
> static B const b = A::E;
> };
>
> A::B const A::b;
>
> int main()
> {
> std::cerr << A::b << "\n";
> }
>
> Rob.


so I consider that it is not necessary to specialize syntax of "static
const int(long...)", haha
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
GL2 better than the XLs? Consumer grade HDs better than pro-sumer Mini DVs? dh@. DVD Video 1 08-28-2008 07:20 PM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Casting int'** to 'const int * const * const' dosn't work, why? Jonas.Holmsten@gmail.com C Programming 11 07-01-2007 06:16 PM
Is splint really better than lint? Is there a better tool than splint? Peter Bencsik C Programming 2 09-21-2006 10:02 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57