C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Q: Using an extern const size_t?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Jakob Bieling
Guest





PostPosted: Sat Feb 28, 2004 4:17 pm    Post subject: Q: Using an extern const size_t? Reply with quote



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)


Back to top
Jakob Bieling
Guest





PostPosted: Sat Feb 28, 2004 4:21 pm    Post subject: Re: Using an extern const size_t? Reply with quote



"Jakob Bieling" <netsurf (AT) gmy (DOT) net> wrote


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)


Back to top
John Harrison
Guest





PostPosted: Sat Feb 28, 2004 4:22 pm    Post subject: Re: Using an extern const size_t? Reply with quote




"Jakob Bieling" <netsurf (AT) gmy (DOT) net> wrote

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

Quote:
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



Back to top
Jakob Bieling
Guest





PostPosted: Sat Feb 28, 2004 4:29 pm    Post subject: Re: Using an extern const size_t? Reply with quote

"John Harrison" <john_andronicus (AT) hotmail (DOT) com> wrote

Quote:

"Jakob Bieling" <netsurf (AT) gmy (DOT) net> wrote in message
news:c1qeta$ig9$00$1 (AT) news (DOT) t-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 ;)

Quote:
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)



Back to top
Artie Gold
Guest





PostPosted: Sat Feb 28, 2004 5:47 pm    Post subject: Re: Using an extern const size_t? Reply with quote

Jakob Bieling wrote:
Quote:
"John Harrison" <john_andronicus (AT) hotmail (DOT) com> wrote in message
news:c1qf7i$1m1avc$1 (AT) ID-196037 (DOT) news.uni-berlin.de...

"Jakob Bieling" <netsurf (AT) gmy (DOT) net> wrote in message
news:c1qeta$ig9$00$1 (AT) news (DOT) t-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!"

Back to top
Jakob Bieling
Guest





PostPosted: Sat Feb 28, 2004 6:05 pm    Post subject: Re: Using an extern const size_t? Reply with quote

"Artie Gold" <artiegold (AT) austin (DOT) rr.com> wrote


Quote:
--- 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)



Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.