Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

static const vs. const static

People also ask

Is it static const or const static?

They mean exactly the same thing. You're free to choose whichever you think is easier to read. In C, you should place static at the start, but it's not yet required.

What is the difference between const and static const?

The const variable is basically used for declaring a constant value that cannot be modified. A static keyword is been used to declare a variable or a method as static. A const keyword is been used to assign a constant or a fixed value to a variable.

What is a static const?

“static const” is basically a combination of static(a storage specifier) and const(a type qualifier). The static determines the lifetime and visibility/accessibility of the variable.

Should I use const or static?

const is a constant value, and cannot be changed. It is compiled into the assembly. static means that it is a value not related to an instance, and it can be changed at run-time (since it isn't readonly ). So if the values are never changed, use consts.


They mean exactly the same thing. You're free to choose whichever you think is easier to read.

In C, you should place static at the start, but it's not yet required. I'm not sure if C++ followed C in this regard.

6.11.5 Storage-class specifiers

1 The placement of a storage-class specifier other than at the beginning of the declaration specifiers in a declaration is an obsolescent feature.


static, const (here, anyway) and the type (e.g. int) are all part of the declaration specifier. Historically, the declaration specifier was an unordered list of keywords and type names, so:

static unsigned int const var;
static unsigned const int var;
static int unsigned const var;
static int const unsigned var;
static const unsigned int var;
static const int unsigned var;
unsigned static int const var;
unsigned static const int var;
unsigned int static const var;
unsigned int const static var;
unsigned const static int var;
unsigned const int static var;
int static unsigned const var;
int static const unsigned var;
int unsigned static const var;
int unsigned const static var;
int const static unsigned var;
int const unsigned static var;
const static unsigned int var;
const static int unsigned var;
const unsigned static int var;
const unsigned int static var;
const int static unsigned var;
const int unsigned static var;

were all legal, and all meant the same thing.

I think that this is still the case, both in C and in C++, but if I'm not mistaken, C has deprecated putting the storage class specifier (static) any where but at the beginning. This is at any rate an almost universal convention, so you should normally put the static (and extern, etc.) at the start.

Note too that being unordered only applies to the declaration specifier. Within the declarators which follow, the cv-qualifier(s) must follow what they qualify; for reasons of orthogonality, you should normally always put the cv-qualifiers after what they modify (i.e. int const, and not const int).

Finally, it seems to be a widespread convention to present the type modifiers before the type, with the signedness modifier (signed or unsigned) preceding the length modifier (short, long or long long). It's also fairly frequent to drop the int if a modifier is present, so people write unsigned, rather than unsigned int, and long, rather than long int. This is far from universal, however.

Given this, the first way the declaration is written, above, is preferred, although it is quite acceptable to drop the int.


They are the same. But I would always go for Option 1 for a simple reason that the keywords const and int fit better when juxtaposed as they define the datatype. Where as the keyword static defines the accessibility of that variable.


They are the same. See this discussion: http://bytes.com/topic/c/answers/140177-const-static-vs-static-const