"static const int" versus "const int"

Discussion in 'C++' started by John Goche, Nov 11, 2011.

  1. John Goche

    John Goche Guest

    Hello,

    I would like to ask what the difference is between
    "static const int" versus "const int". I mean of course
    the former goes in the static data segment of code
    whereas the latter is pushed on the stack, but since
    a const does not change it might as well be declared
    static, but to what advantage.

    What I am asking is how to decide whether to use
    "static const int" versus "const int".

    Thanks,

    John Goche
    John Goche, Nov 11, 2011
    #1
    1. Advertising

  2. On 11/11/2011 11:39 AM, John Goche wrote:
    > I would like to ask what the difference is between
    > "static const int" versus "const int". I mean of course
    > the former goes in the static data segment of code
    > whereas the latter is pushed on the stack, but since
    > a const does not change it might as well be declared
    > static, but to what advantage.
    >
    > What I am asking is how to decide whether to use
    > "static const int" versus "const int".


    In what scope? It matters a lot.

    V
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Nov 11, 2011
    #2
    1. Advertising

  3. John Goche

    John Goche Guest

    On Nov 11, 6:31 pm, Victor Bazarov <> wrote:
    > On 11/11/2011 11:39 AM, John Goche wrote:
    >
    > > I would like to ask what the difference is between
    > > "static const int" versus "const int". I mean of course
    > > the former goes in the static data segment of code
    > > whereas the latter is pushed on the stack, but since
    > > a const does not change it might as well be declared
    > > static, but to what advantage.

    >
    > > What I am asking is how to decide whether to use
    > > "static const int" versus "const int".

    >
    > In what scope?  It matters a lot.


    I forgot to mention: inside the block scope
    of the body of a function.

    Also, as a separate question, what's wrong with

    const int foo = 1;

    const int bar = foo + 1;

    The compiler replies with: error: initializer element is not constant

    Of course I could use the preprocessor, but the preprocessor is
    not very popular in C++ as it is in C, so what should I do, compose
    a function:

    const int bar() {
    return foo + 1;
    }

    Seems silly to me.

    Thanks,

    John Goche
    John Goche, Nov 11, 2011
    #3
  4. John Goche

    John Goche Guest

    On Nov 11, 7:39 pm, Leigh Johnston <> wrote:

    > > const int foo = 1;

    >
    > > const int bar = foo + 1;

    >
    > > The compiler replies with: error: initializer element is not constant

    >
    > That code is not ill-formed so you are either a) lying or b) have a
    > broken compiler.
    >
    > /Leigh


    In this case the code was declared outside of any function as a
    global variable, and then I get the error, otherwise you are right
    it would compile fine inside a function.

    Anyways, I still don't have an answer to the two questions I posted.

    Thank you for your replies though,

    Regards,

    John Goche
    John Goche, Nov 11, 2011
    #4
  5. On 11/11/2011 1:30 PM, John Goche wrote:
    > On Nov 11, 6:31 pm, Victor Bazarov<> wrote:
    >> On 11/11/2011 11:39 AM, John Goche wrote:
    >>
    >>> I would like to ask what the difference is between
    >>> "static const int" versus "const int". I mean of course
    >>> the former goes in the static data segment of code
    >>> whereas the latter is pushed on the stack, but since
    >>> a const does not change it might as well be declared
    >>> static, but to what advantage.

    >>
    >>> What I am asking is how to decide whether to use
    >>> "static const int" versus "const int".

    >>
    >> In what scope? It matters a lot.

    >
    > I forgot to mention: inside the block scope
    > of the body of a function.


    There is no difference from the behavior POV. Code-generation-wise, an
    automatic constant can easily be replaced by the compiler with its value
    when used in an expression (so no storage is usually wasted on it at
    all), and a static object will cause some storage allocation and
    possibly the use of that storage when the variable is used in an
    expression. Should you care about that? I don't know.

    > [..]


    V
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Nov 11, 2011
    #5
    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. Matthew Louden
    Replies:
    1
    Views:
    6,912
    Scott M.
    Oct 11, 2003
  2. Ajax Chelsea
    Replies:
    3
    Views:
    16,889
    Ajax Chelsea
    Dec 5, 2003
  3. ThazKool
    Replies:
    1
    Views:
    445
  4. Replies:
    11
    Views:
    1,101
  5. Paul Butcher
    Replies:
    12
    Views:
    709
    Gary Wright
    Nov 28, 2007
Loading...

Share This Page