473,770 Members | 1,899 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

const int not constant expression?

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_cal lback;

/* globals.cpp */
const int g_tray_icon_cal lback = 4711;

Now, when I tried to use that variable as a case label in switch statement,
I got:
main_dialog_pro cedure.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
Jul 19 '05 #1
5 10223
William Payne wrote:
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_cal lback;

/* globals.cpp */
const int g_tray_icon_cal lback = 4711;

Now, when I tried to use that variable as a case label in switch statement,
I got:
main_dialog_pro cedure.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_cal lback = 4711;

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

--
Best regards,
Andrey Tarasevich
Brainbench C and C++ Programming MVP

Jul 19 '05 #2
"William Payne" <mi************ **@student.liu. se> wrote in message
news:bi******** **@news.island. liu.se...
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_cal lback;

/* globals.cpp */
const int g_tray_icon_cal lback = 4711;

Now, when I tried to use that variable as a case label in switch statement,
I got:
main_dialog_pro cedure.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_cal lback 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_cal lback = 4711;

--
ES Kim
Jul 19 '05 #3

"Andrey Tarasevich" <an************ **@hotmail.com> wrote in message
news:vk******** ****@news.super news.com...
William Payne wrote:
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_cal lback;

/* globals.cpp */
const int g_tray_icon_cal lback = 4711;

Now, when I tried to use that variable as a case label in switch statement, I got:
main_dialog_pro cedure.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_cal lback = 4711;

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

--
Best regards,
Andrey Tarasevich
Brainbench C and C++ Programming MVP


Thank you for the help

// William Payne
Jul 19 '05 #4
ES Kim wrote:
"William Payne" <mi************ **@student.liu. se> wrote in message
news:bi******** **@news.island. liu.se...
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_cal lback;

/* globals.cpp */
const int g_tray_icon_cal lback = 4711;

Now, when I tried to use that variable as a case label in switch statement,
I got:
main_dialog_pro cedure.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_cal lback defined in globals.cpp has
internal linkage.


That's not true. Provided the declaration of 'g_tray_icon_ca llback' in
'globals.h' is visible in 'globals.cpp' (i.e. 'globals.h' is included
into 'globals.cpp'), constant object 'g_tray_icon_ca llback' 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_ca llback' is
declared in 'globals.cpp' with explicit 'extern' specifier.
Andrey provided a solution.
My solution actually changes the linkage of 'g_tray_icon_ca llback' to
internal, meaning that each translation unit will get its own completely
independent instance of 'g_tray_icon_ca llback'.
Another is to make it have external
linkage.

/* globals.cpp */
extern const int g_tray_icon_cal lback = 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.

--
Best regards,
Andrey Tarasevich
Brainbench C and C++ Programming MVP

Jul 19 '05 #5
"Andrey Tarasevich" <an************ **@hotmail.com> wrote in message
news:dO******** ************@co mcast.com...

[snip]

You're right. Thanks for your concrete explanation.

--
ES Kim
Jul 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

31
2437
by: Ben | last post by:
For many times, I've found myself changing my member variables from const back to non-const. No matter how good the original objective was, there was always at least one reason not to use const members. (swap, storing it in a container, etc.) Whereas in Java, 80% of the case, I would want "final" for my instance variables. It makes me think that is "const member variable" ever useful in C++? Maybe because of the value semantics of C++...
16
41776
by: herbertF | last post by:
Hi guys, In a program (not my own) I encountered the declaration of a constant pointer to an array consisting of two other const pointers to arrays. Not quite sure why they do it so complicated, but is it legal? Most compilers accept it, but one doesn't recognize the rhs as a constant. What are the requirements for the rhs in the declaration of a const pointer? Is the following program legal C? int main(int argc, char *argv) {
13
2574
by: devdatta_clc | last post by:
Hi C experts I've a bunch of questions. Consider this simplified piece of code. const int a = 10; int main () { static int b = a;
9
2622
by: sarathy | last post by:
Hi, Can anyone just help me with what exactly is constant expression. I read the section on Constant expression in K&R2. i am not fully clear with it.
4
3311
by: Rui.Hu719 | last post by:
Hi, All: I read the following passage from a book: "There are three exceptions to the rule that headers should not contain definitions: classes, const objects whose value is known at compile time, and inline functions are all defined in headers. " Can someone explain to me why some of the const objects must be defined in the header file?
10
2792
by: d3x0xr | last post by:
---- Section 1 ---- ------ x.c int main( void ) { char **a; char const *const *b; b = a; // line(9)
4
3083
by: jaime | last post by:
Hi again all. Given the line: const int x=5; Can I then use "x" as a constant expression? (By "constant expression", I mean "constant expression" as defined in the C99 standard) I've been searching google for 2 days now trying to answer this myself, and I'm just getting more and more confused (some things I read make me think "yes", while some things I read make me think "no").
10
5974
by: Stephen Howe | last post by:
Hi Just going over some grey areas in my knowledge in C++: 1) If I have const int SomeConst = 1; in a header file, it is global, and it is included in multiple translations units, but it is unused, I know that it does not take up storage in the
7
230
by: abendstund | last post by:
Hi, I have the following code and trouble with ambiguity due to operator overloading.. The code is also at http://paste.nn-d.de/441 snip>>
16
1927
by: arnuld | last post by:
I have declared an int as const but compiler still says that it is not a const: include <stdio.h> #include <stdlib.h> int main() { const int MAXSIZE = 100;
0
9617
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10036
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9904
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8929
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7451
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6710
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5481
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.