const static Vs. static const

4 views
Skip to first unread message

Dave

unread,
May 21, 2005, 8:24:52 PM5/21/05
to
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!


Ron Natalie

unread,
May 21, 2005, 8:47:27 PM5/21/05
to
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.

John Carson

unread,
May 21, 2005, 9:51:37 PM5/21/05
to
"Ron Natalie" <r...@spamcop.net> wrote in message
news:428fd52f$0$6629$9a6e...@news.newshosting.com


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

--
John Carson

Larry I Smith

unread,
May 21, 2005, 10:34:31 PM5/21/05
to

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

--
Anti-spam address, change each 'X' to '.' to reply directly.

Teddy

unread,
May 21, 2005, 10:40:08 PM5/21/05
to
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

Alf P. Steinbach

unread,
May 21, 2005, 11:11:12 PM5/21/05
to
* Teddy:

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


--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

Teddy

unread,
May 21, 2005, 11:17:00 PM5/21/05
to
yes, there should be six valid combinations.
but do they have the same meaning ?
i think i'm a little bit confused.

Dave

unread,
May 21, 2005, 11:37:35 PM5/21/05
to

"Teddy" <duand...@hotmail.com> wrote in message
news:1116731820....@g49g2000cwa.googlegroups.com...

> 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.


Rolf Magnus

unread,
May 22, 2005, 9:24:03 AM5/22/05
to
Larry I Smith wrote:

> 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"

Larry I Smith

unread,
May 22, 2005, 12:41:05 PM5/22/05
to
Rolf Magnus wrote:
> Larry I Smith wrote:
>
>>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"
>


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.

Ron Natalie

unread,
May 22, 2005, 6:32:22 PM5/22/05
to
John Carson wrote:

> 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.

Reply all
Reply to author
Forward
0 new messages