static const vs. const static

C++ClassConstants

C++ Problem Overview


I do not understand the difference between these two statements in my C++ class:

class MyClass {
  public:
  private:
     static const int var = 0;            // Option 1
     const static int var = 0;            // Option 2
};

What is the difference b/w Option 1 and Option 2?? They both compile.

C++ Solutions


Solution 1 - C++

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.

Solution 2 - C++

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.

Solution 3 - C++

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.

Solution 4 - C++

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBigBrownBear00View Question on Stackoverflow
Solution 1 - C++user743382View Answer on Stackoverflow
Solution 2 - C++James KanzeView Answer on Stackoverflow
Solution 3 - C++Ed HealView Answer on Stackoverflow
Solution 4 - C++nutrinaView Answer on Stackoverflow