const static Vs. static const

Discussion in 'C++' started by Dave, May 22, 2005.

  1. Dave

    Dave Guest

    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!
     
    Dave, May 22, 2005
    #1
    1. Advertising

  2. Dave

    Ron Natalie Guest

    Dave wrote:
    > 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.
     
    Ron Natalie, May 22, 2005
    #2
    1. Advertising

  3. Dave

    John Carson Guest

    "Ron Natalie" <> wrote in message
    news:428fd52f$0$6629$
    > Dave wrote:
    >> 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.



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

    --
    John Carson
     
    John Carson, May 22, 2005
    #3
  4. Ron Natalie wrote:
    > Dave wrote:
    >> 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.


    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.
     
    Larry I Smith, May 22, 2005
    #4
  5. Dave

    Teddy Guest

    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
     
    Teddy, May 22, 2005
    #5
  6. * 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.


    --
    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?
     
    Alf P. Steinbach, May 22, 2005
    #6
  7. Dave

    Teddy Guest

    yes, there should be six valid combinations.
    but do they have the same meaning ?
    i think i'm a little bit confused.
     
    Teddy, May 22, 2005
    #7
  8. Dave

    Dave Guest

    "Teddy" <> wrote in message
    news:...
    > 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.
     
    Dave, May 22, 2005
    #8
  9. Dave

    Rolf Magnus Guest

    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"

    > static const int ARRAY_SIZE2 = 4;
    >
    > This statement produces the compile error:
    > "error: syntax error before `static'"
    >
    > const int static ARRAY_SIZE3 = 4;
     
    Rolf Magnus, May 22, 2005
    #9
  10. 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.

    Regards,
    Larry

    --
    Anti-spam address, change each 'X' to '.' to reply directly.
     
    Larry I Smith, May 22, 2005
    #10
  11. Dave

    Ron Natalie Guest

    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.
     
    Ron Natalie, May 22, 2005
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rakesh Sinha
    Replies:
    4
    Views:
    1,854
    Rakesh Sinha
    Jan 13, 2005
  2. Replies:
    11
    Views:
    1,109
  3. Javier
    Replies:
    2
    Views:
    568
    James Kanze
    Sep 4, 2007
  4. er
    Replies:
    3
    Views:
    386
  5. 0m
    Replies:
    26
    Views:
    1,124
    Tim Rentsch
    Nov 10, 2008
Loading...

Share This Page