const static Vs. static const

D

Dave

const static int ARRAY_SIZE = 4;

Comeau online gives this warning:
"ComeauTest.c", line 10: warning: storage class is not first const static
int ARRAY_SIZE = 4;



Why is static const preferable to const static?

Thanks!
 
R

Ron Natalie

Dave said:
const static int ARRAY_SIZE = 4;

Comeau online gives this warning:
"ComeauTest.c", line 10: warning: storage class is not first const static
int ARRAY_SIZE = 4;



Why is static const preferable to const static?
Because it's not legal. The grammar doewsn't allow the
storage-clas-specifier to be inserted inside the type specifier.
Comeaus error message is hwoever wrong (or at least misleading.
The storage class specifier need not be first:

const int static ARRAY_SIZE = 4;

is perfectly legal. Inserting static between const and int
is not however.
 
J

John Carson

Ron Natalie said:
Because it's not legal. The grammar doewsn't allow the
storage-clas-specifier to be inserted inside the type specifier.
Comeaus error message is hwoever wrong (or at least misleading.
The storage class specifier need not be first:

const int static ARRAY_SIZE = 4;

is perfectly legal. Inserting static between const and int
is not however.


Are you sure? Comeau compiles both versions with the same warning. VC++ 7.1
compiles both versions with no warning.
 
L

Larry I Smith

Ron said:
Because it's not legal. The grammar doewsn't allow the
storage-clas-specifier to be inserted inside the type specifier.
Comeaus error message is hwoever wrong (or at least misleading.
The storage class specifier need not be first:

const int static ARRAY_SIZE = 4;

is perfectly legal. Inserting static between const and int
is not however.

GCC g++ v3.3.4 compiles both of these statements without error
or warning (even with '-Wall -ansi -pedantic' compile options):

const static int ARRAY_SIZE1 = 4;
static const int ARRAY_SIZE2 = 4;

This statement produces the compile error:
"error: syntax error before `static'"

const int static ARRAY_SIZE3 = 4;

Regards,
Larry
 
T

Teddy

const static int ARRAY_SIZE1 = 4;
static const int ARRAY_SIZE2 = 4;
const int static ARRAY_SIZE3 = 4;

VC2005 BETA2 compiles all the three statements without error or warning
 
A

Alf P. Steinbach

* Teddy:
const static int ARRAY_SIZE1 = 4;
static const int ARRAY_SIZE2 = 4;
const int static ARRAY_SIZE3 = 4;

VC2005 BETA2 compiles all the three statements without error or warning

I'm unable to find any restriction on the order in the standard; assuming
there is no such there should be six valid combinations.
 
T

Teddy

yes, there should be six valid combinations.
but do they have the same meaning ?
i think i'm a little bit confused.
 
D

Dave

Teddy said:
yes, there should be six valid combinations.
but do they have the same meaning ?
i think i'm a little bit confused.

If they're all valid, then yes they would have the same meaning. The
questions centers around whether or not they are all valid.
 
R

Rolf Magnus

Larry said:
GCC g++ v3.3.4 compiles both of these statements without error
or warning (even with '-Wall -ansi -pedantic' compile options):

You forgot to add -W to the options.
const static int ARRAY_SIZE1 = 4;

With -W, it says:
"warning: `static' is not at beginning of declaration"
 
L

Larry I Smith

Rolf said:
You forgot to add -W to the options.


With -W, it says:
"warning: `static' is not at beginning of declaration"


Hmm, you are correct.

Thanks for the info about '-W'.

The doc supplied with g++ v3.3.4 ('info GCC') does not
mention '-W'. It does discuss many '-W...' options
that can all be enabled via '-Wall' and disabled with
'-w' (lowercase).

It seems that '-W -Wall' needs to be specified to
turn on all warnings. The '-Wall' switch name is
misleading - it doesn't enable 'all' warnings.

Sadly, it appears that the GCC docs are incomplete.

Regards,
Larry
 
R

Ron Natalie

John said:
Are you sure? Comeau compiles both versions with the same warning. VC++
7.1 compiles both versions with no warning.

No actually, it looks like I was wrong. Comeau's still wrong.
The decl-specifier-seq can be made up of an arbitrary order of
type-specifiers and storage-class-specifiers. The const (a
CV-qualifer) and the int (a simple-type-name) are type-specifiers.
Static is a storage-class-specifier. I can't find any applicable
semantic restriction that applies.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top