Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Q: Using an extern const size_t?

Reply
Thread Tools

Q: Using an extern const size_t?

 
 
Jakob Bieling
Guest
Posts: n/a
 
      02-28-2004
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)


 
Reply With Quote
 
 
 
 
Jakob Bieling
Guest
Posts: n/a
 
      02-28-2004
"Jakob Bieling" <(E-Mail Removed)> wrote in message
news:c1qeta$ig9$00$(E-Mail Removed)-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)


 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      02-28-2004

"Jakob Bieling" <(E-Mail Removed)> wrote in message
news:c1qeta$ig9$00$(E-Mail Removed)-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


 
Reply With Quote
 
Jakob Bieling
Guest
Posts: n/a
 
      02-28-2004
"John Harrison" <(E-Mail Removed)> wrote in message
news:c1qf7i$1m1avc$(E-Mail Removed)-berlin.de...
>
> "Jakob Bieling" <(E-Mail Removed)> wrote in message
> news:c1qeta$ig9$00$(E-Mail Removed)-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)


 
Reply With Quote
 
Artie Gold
Guest
Posts: n/a
 
      02-28-2004
Jakob Bieling wrote:
> "John Harrison" <(E-Mail Removed)> wrote in message
> news:c1qf7i$1m1avc$(E-Mail Removed)-berlin.de...
>
>>"Jakob Bieling" <(E-Mail Removed)> wrote in message
>>news:c1qeta$ig9$00$(E-Mail Removed)-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!"
 
Reply With Quote
 
Jakob Bieling
Guest
Posts: n/a
 
      02-28-2004
"Artie Gold" <(E-Mail Removed)> wrote in message
news:c1qk80$1l65d4$(E-Mail Removed)-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)


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Difference of extern short *x and extern short x[]? Andre C Programming 5 07-17-2012 07:38 PM
is const necessary in eg int compar(const void *, const void *) lovecreatesbeauty@gmail.c0m C Programming 26 11-10-2008 09:47 PM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Casting int'** to 'const int * const * const' dosn't work, why? Jonas.Holmsten@gmail.com C Programming 11 07-01-2007 06:16 PM
extern const char * vs. extern const char []http://tinyurl.com/47e3k Thomas Matthews C++ 5 08-02-2004 10:36 AM



Advertisments