const int not constant expression?

W

William Payne

Hello, in my application (which spans approximately 20 source files and a
2-3 thousand lines) I have a few global varialbes, variables that never
change their values. One of them is declared like this:
/* globals.h */
extern const int g_tray_icon_callback;

/* globals.cpp */
const int g_tray_icon_callback = 4711;

Now, when I tried to use that variable as a case label in switch statement,
I got:
main_dialog_procedure.cpp:64: error: case label does not reduce to an
integer
constant

So, I replaced the const int with a #define and it compiles, but why can't I
use a const int? I thought const would be const, but I guess not...

// William Payne
 
A

Andrey Tarasevich

William said:
Hello, in my application (which spans approximately 20 source files and a
2-3 thousand lines) I have a few global varialbes, variables that never
change their values. One of them is declared like this:
/* globals.h */
extern const int g_tray_icon_callback;

/* globals.cpp */
const int g_tray_icon_callback = 4711;

Now, when I tried to use that variable as a case label in switch statement,
I got:
main_dialog_procedure.cpp:64: error: case label does not reduce to an
integer
constant

So, I replaced the const int with a #define and it compiles, but why can't I
use a const int? I thought const would be const, but I guess not...

You can. But in order to be able to use this 'const int' variable in a
case label, you have to declare it _with_ _initilaizer_ in the same
translation unit where your 'switch' is located.

Just put

const int g_tray_icon_callback = 4711;

into your 'globals.h' file and remove the definition from 'globals.cpp'.
 
E

ES Kim

William Payne said:
Hello, in my application (which spans approximately 20 source files and a
2-3 thousand lines) I have a few global varialbes, variables that never
change their values. One of them is declared like this:
/* globals.h */
extern const int g_tray_icon_callback;

/* globals.cpp */
const int g_tray_icon_callback = 4711;

Now, when I tried to use that variable as a case label in switch statement,
I got:
main_dialog_procedure.cpp:64: error: case label does not reduce to an
integer
constant

So, I replaced the const int with a #define and it compiles, but why can't I
use a const int? I thought const would be const, but I guess not...

// William Payne

It's because g_tray_icon_callback defined in globals.cpp has
internal linkage. const objects have internal linkage by default.
Andrey provided a solution. Another is to make it have external
linkage.

/* globals.cpp */
extern const int g_tray_icon_callback = 4711;
 
W

William Payne

Andrey Tarasevich said:
You can. But in order to be able to use this 'const int' variable in a
case label, you have to declare it _with_ _initilaizer_ in the same
translation unit where your 'switch' is located.

Just put

const int g_tray_icon_callback = 4711;

into your 'globals.h' file and remove the definition from 'globals.cpp'.

Thank you for the help

// William Payne
 
A

Andrey Tarasevich

ES said:
It's because g_tray_icon_callback defined in globals.cpp has
internal linkage.

That's not true. Provided the declaration of 'g_tray_icon_callback' in
'globals.h' is visible in 'globals.cpp' (i.e. 'globals.h' is included
into 'globals.cpp'), constant object 'g_tray_icon_callback' has external
linkage since it is declared with 'extern' specifier.
const objects have internal linkage by default.

Yes. But in this case it is not "by default". 'g_tray_icon_callback' is
declared in 'globals.cpp' with explicit 'extern' specifier.
Andrey provided a solution.

My solution actually changes the linkage of 'g_tray_icon_callback' to
internal, meaning that each translation unit will get its own completely
independent instance of 'g_tray_icon_callback'.
Another is to make it have external
linkage.

/* globals.cpp */
extern const int g_tray_icon_callback = 4711;

That's not necessary. The declaration in 'globals.h' is sufficient to
change the linkage of this object.

The whole point of my solution is to make the initializer ('= 4711')
visible in all translation units. Your solution doesn't do that, which
means that it won't solve anything.
 

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

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,835
Latest member
KetoRushACVBuy

Latest Threads

Top