Q: Using an extern const size_t?

Discussion in 'C++' started by Jakob Bieling, Feb 28, 2004.

  1. Hi,

    I have the following scenario:

    --- file1.cpp ---

    size_t const array_size = 10;

    --- end file1.cpp ---

    --- file2.cpp ---

    class test
    {
    int m_nums [array_size];
    };

    --- end file2.cpp ---

    My compiler, VC++7.1, complains about not having a constant expression
    for the array. Why can the array size not be 'calculated' at compile-time?
    Or is it just a bug in VC++?

    Thanks!
    --
    jb

    (replace y with x if you want to reply by e-mail)
    Jakob Bieling, Feb 28, 2004
    #1
    1. Advertising

  2. Re: Using an extern const size_t?

    "Jakob Bieling" <> wrote in message
    news:c1qeta$ig9$00$-online.com...

    I forgot a line:

    --- file1.cpp ---

    size_t const array_size = 10;

    --- end file1.cpp ---

    --- file2.cpp ---

    extern size_t const array_size;

    class test
    {
    int m_nums [array_size];
    };

    --- end file2.cpp ---

    Sorry about that
    --
    jb

    (replace y with x if you want to reply by e-mail)
    Jakob Bieling, Feb 28, 2004
    #2
    1. Advertising

  3. Re: Using an extern const size_t?

    "Jakob Bieling" <> wrote in message
    news:c1qeta$ig9$00$-online.com...
    > Hi,
    >
    > I have the following scenario:
    >
    > --- file1.cpp ---
    >
    > size_t const array_size = 10;
    >
    > --- end file1.cpp ---
    >
    > --- file2.cpp ---
    >


    I guessing that you missed out

    extern const size_t array_size;

    here.

    > class test
    > {
    > int m_nums [array_size];
    > };
    >
    > --- end file2.cpp ---
    >
    > My compiler, VC++7.1, complains about not having a constant expression
    > for the array. Why can the array size not be 'calculated' at compile-time?
    > Or is it just a bug in VC++?
    >


    Put

    size_t const array_size = 10;

    in a header file and include that header file in file1.cpp and file2.cpp.
    This is OK because in C++ constants have internal linkage by default so you
    won't get an error about array_size being declared twice.

    john
    John Harrison, Feb 28, 2004
    #3
  4. Re: Using an extern const size_t?

    "John Harrison" <> wrote in message
    news:c1qf7i$1m1avc$-berlin.de...
    >
    > "Jakob Bieling" <> wrote in message
    > news:c1qeta$ig9$00$-online.com...
    > > Hi,
    > >
    > > I have the following scenario:
    > >
    > > --- file1.cpp ---
    > >
    > > size_t const array_size = 10;
    > >
    > > --- end file1.cpp ---
    > >
    > > --- file2.cpp ---
    > >

    >
    > I guessing that you missed out
    >
    > extern const size_t array_size;
    >
    > here.


    Yes, see my reply to myself ;)

    > > class test
    > > {
    > > int m_nums [array_size];
    > > };
    > >
    > > --- end file2.cpp ---
    > >
    > > My compiler, VC++7.1, complains about not having a constant

    expression
    > > for the array. Why can the array size not be 'calculated' at

    compile-time?
    > > Or is it just a bug in VC++?
    > >

    >
    > Put
    >
    > size_t const array_size = 10;
    >
    > in a header file and include that header file in file1.cpp and file2.cpp.
    > This is OK because in C++ constants have internal linkage by default so

    you
    > won't get an error about array_size being declared twice.


    Right, but why does the above example not work? Theoretically, it
    should, or what am I missing?
    --
    jb

    (replace y with x if you want to reply by e-mail)
    Jakob Bieling, Feb 28, 2004
    #4
  5. Jakob Bieling

    Artie Gold Guest

    Re: Using an extern const size_t?

    Jakob Bieling wrote:
    > "John Harrison" <> wrote in message
    > news:c1qf7i$1m1avc$-berlin.de...
    >
    >>"Jakob Bieling" <> wrote in message
    >>news:c1qeta$ig9$00$-online.com...
    >>
    >>>Hi,
    >>>
    >>> I have the following scenario:
    >>>
    >>>--- file1.cpp ---
    >>>
    >>>size_t const array_size = 10;
    >>>
    >>>--- end file1.cpp ---
    >>>
    >>>--- file2.cpp ---
    >>>

    >>
    >>I guessing that you missed out
    >>
    >>extern const size_t array_size;
    >>
    >>here.

    >
    >
    > Yes, see my reply to myself ;)
    >
    >
    >>>class test
    >>>{
    >>> int m_nums [array_size];
    >>>};
    >>>
    >>>--- end file2.cpp ---
    >>>
    >>> My compiler, VC++7.1, complains about not having a constant

    >
    > expression
    >
    >>>for the array. Why can the array size not be 'calculated' at

    >
    > compile-time?
    >
    >>>Or is it just a bug in VC++?
    >>>

    >>
    >>Put
    >>
    >>size_t const array_size = 10;
    >>
    >>in a header file and include that header file in file1.cpp and file2.cpp.
    >>This is OK because in C++ constants have internal linkage by default so

    >
    > you
    >
    >>won't get an error about array_size being declared twice.

    >
    >
    > Right, but why does the above example not work? Theoretically, it
    > should, or what am I missing?


    You are missing the fact that the value of `array_size' will not be
    known until link time (as it is defined in a different translation unit).

    HTH,
    --ag

    --
    Artie Gold -- Austin, Texas

    "Yeah. It's an urban legend. But it's a *great* urban legend!"
    Artie Gold, Feb 28, 2004
    #5
  6. Re: Using an extern const size_t?

    "Artie Gold" <> wrote in message
    news:c1qk80$1l65d4$-berlin.de...

    > >>>--- file1.cpp ---
    > >>>
    > >>>size_t const array_size = 10;
    > >>>
    > >>>--- end file1.cpp ---
    > >>>
    > >>>--- file2.cpp ---


    > >>extern const size_t array_size;


    > >>>class test
    > >>>{
    > >>> int m_nums [array_size];
    > >>>};
    > >>>
    > >>>--- end file2.cpp ---


    > > Right, but why does the above example not work? Theoretically, it
    > > should, or what am I missing?

    >
    > You are missing the fact that the value of `array_size' will not be
    > known until link time (as it is defined in a different translation unit).


    Oh .. makes sense now, yes!

    Thanks!
    --
    jb

    (replace y with x if you want to reply by e-mail)
    Jakob Bieling, Feb 28, 2004
    #6
    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. Thomas Matthews
    Replies:
    5
    Views:
    2,413
    tom_usenet
    Aug 2, 2004
  2. Javier
    Replies:
    2
    Views:
    559
    James Kanze
    Sep 4, 2007
  3. Alex Vinokur
    Replies:
    9
    Views:
    787
    James Kanze
    Oct 13, 2008
  4. Alex Vinokur
    Replies:
    1
    Views:
    574
  5. Andre
    Replies:
    5
    Views:
    535
    Keith Thompson
    Jul 17, 2012
Loading...

Share This Page