473,395 Members | 1,530 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

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

is it necessary that define special initialization syntax for "static const int"?
Jul 22 '05 #1
3 12329
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
Jul 22 '05 #2
Phlip wrote in news:ey*******************@newssvr32.news.prodigy. 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::x;
}

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/
Jul 22 '05 #3
Rob Williscroft <rt*@freenet.REMOVE.co.uk> wrote in message news:<Xn**********************************@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
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: heinquoi | last post by:
hello, i have the code: class window { static const HWND hWnd; static const HINSTANCE hInst; // ... next code here window (HINSTANCE ); }
0
by: Patrick Guio | last post by:
Dear all, I wonder whether anyone might have a better idea/solution to the following. I need an associative container <int,string> for a limited and defined number of values (enum-like) but ir...
6
by: Fao | last post by:
Hi, I am in my first year of C++ in college and my professor wants me to Write a Program with multiple functions,to input two sets of user-defined data types: One type named 'Sign' declared by...
2
by: Susan Baker | last post by:
Hi, I got this error msg whilst building some classes. It is realatively asy to fix. But I just wondered, does anyone know the technical reason why one can't initialiaze a const static...
5
by: vilhelm.sjoberg | last post by:
Hello, I am a resonably confident C programmer, but not very sure about the dark corners of C++. Recently, I had G++ give me a strange error. The program in question is in essence: struct...
72
by: Paminu | last post by:
In math this expression: (a < b) && (b < c) would be described as: a < b < c But why is it that in C these two expressions evaluate to something different for the same values of a, b and...
4
by: babylon | last post by:
I can't do public const short test = new short{1000,2000,3000}; any suggestions? thx
16
by: Chris | last post by:
Looking at some code I see a declaration inside a function like static const string s("some string"); Does the static serve any purpose here?
6
by: .rhavin grobert | last post by:
hello;-) i frequently need the following construction: ReturnParam § Function() § { /...do something.../ someType var § = something; /...do something.../ return something;
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.