Q: Using an extern const size_t?

J

Jakob Bieling

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!
 
J

Jakob Bieling

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
 
J

John Harrison

Jakob Bieling said:
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
 
J

Jakob Bieling

John Harrison said:
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?
 
A

Artie Gold

Jakob said:
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
 
J

Jakob Bieling

--- 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!
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top