473,569 Members | 2,991 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Can't use object as a case label!

g++ test.cpp -ansi -pedantic -o test.exe
test.cpp: In function `int main()':
test.cpp:22: case label does not reduce to an integer constant

Why won't "operator unsigned int() const" do its job in this... case?!
class Blah
{
private:

unsigned int k;

public:

operator unsigned int() const
{
return k;
}
};

int main()
{
Blah aaa;
Blah bbb;

switch (aaa)
{
case bbb:
;
}
}
-JKop
Jul 22 '05 #1
8 1706
* JKop:
g++ test.cpp -ansi -pedantic -o test.exe
test.cpp: In function `int main()':
test.cpp:22: case label does not reduce to an integer constant


That's because you have used the name of an object as case label, instead
of using an integer expression as case label.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #2
Alf P. Steinbach posted:
* JKop:
g++ test.cpp -ansi -pedantic -o test.exe
test.cpp: In function `int main()': test.cpp:22: case label does not reduce to an integer constant
That's because you have used the name of an object as

case label, instead of using an integer expression as case label.


Ohh... I get it, it has to be a compile-time constant.

Even the following won't work!:

class Blah
{
private:

unsigned int k;

public:

Blah(unsigned int input) : k(input) {}

operator unsigned int() const
{
return k;
}
};

int main()
{
Blah aaa(87);
Blah const bbb(6);

switch (aaa)
{
case bbb:
;
}
}
Most likely as Blah isn't a POD...

-JKop
Jul 22 '05 #3

"JKop" <NU**@NULL.NULL > wrote in message
news:rh******** **********@news .indigo.ie...
Alf P. Steinbach posted:
* JKop:
g++ test.cpp -ansi -pedantic -o test.exe
test.cpp: In function `int main()': test.cpp:22: case label does not reduce to an integer constant
That's because you have used the name of an object as

case label, instead
of using an integer expression as case label.


Ohh... I get it, it has to be a compile-time constant.


No, it only needs to be an integer expression.

e.g.

int x = 42;
switch(x) /* 'x' is not a a compile-time constant */
{
}

Even the following won't work!:

class Blah
{
private:

unsigned int k;

public:

Blah(unsigned int input) : k(input) {}

operator unsigned int() const
{
return k;
}
};

int main()
{
Blah aaa(87);
Blah const bbb(6);

switch (aaa)
{
case bbb:
;
}
}
Most likely as Blah isn't a POD...


'Blah' is not an integer expression.

Try e.g.

unsigned int sw(aaa);
switch(sw)
{
}
-Mike
Jul 22 '05 #4
JKop wrote:
Alf P. Steinbach posted: ....
Ohh... I get it, it has to be a compile-time constant.


Exactly, even thout the compiler (in theory) could determine that the
function will return the same value allways, by defintion, a value
returned from a function is not a compile time constant and hence the
issue you raise.

Now for a total digression ... There are some interesting cases where
the compiler can actually determine at optimization time that the
run-time expression is constant and this is rather interesting.

bool is_little_endia n()
{
int v = 1;

return 1 == * static_cast<cha r*>( static_cast<voi d*>( & v ) );
}

After investigating optimized code generated from GCC it's apparent the
compiler is capable of generating code as if using a constant.

Jul 22 '05 #5
Gianni Mariani posted:
Now for a total digression ... There are some interesting cases where
the compiler can actually determine at optimization time that the
run-time expression is constant and this is rather interesting.

bool is_little_endia n()
{
int v = 1;

return 1 == * static_cast<cha r*>( static_cast<voi d*>( & v ) );
*************** ***
I'd've written:

unsigned int const i = 1;

return 1 == *reinterpret_ca st<const unsigned char* const &>(&i);
*************** ***
}

After investigating optimized code generated from GCC it's apparent the
compiler is capable of generating code as if using a constant.

Sounds good!

-JKop
Jul 22 '05 #6
> 'Blah' is not an integer expression.

Yeah but I was hoping that "operator int() const" would do
its job...
-JKop
Jul 22 '05 #7
"Mike Wahler" <mk******@mkwah ler.net> wrote...

"JKop" <NU**@NULL.NULL > wrote in message
news:rh******** **********@news .indigo.ie...
Alf P. Steinbach posted:
> * JKop:
>> g++ test.cpp -ansi -pedantic -o test.exe
>> test.cpp: In function `int main()': test.cpp:22: case label does not
>> reduce to an integer constant
>
> That's because you have used the name of an object as

case label, instead
> of using an integer expression as case label. ^^^^^^^^^^^^ >


Ohh... I get it, it has to be a compile-time constant.


No, it only needs to be an integer expression.


No, a _case_label_ needs to be a compile-time constant.
[...]


V
Jul 22 '05 #8

"Mike Wahler" <mk******@mkwah ler.net> wrote in message
news:tF******** *********@newsr ead1.news.pas.e arthlink.net...

"JKop" <NU**@NULL.NULL > wrote in message
news:rh******** **********@news .indigo.ie...
Alf P. Steinbach posted:
* JKop:
> g++ test.cpp -ansi -pedantic -o test.exe
> test.cpp: In function `int main()': test.cpp:22: case

label does not
> reduce to an integer constant

That's because you have used the name of an object as

case label, instead
of using an integer expression as case label.


Ohh... I get it, it has to be a compile-time constant.


No, it only needs to be an integer expression.

e.g.

int x = 42;
switch(x) /* 'x' is not a a compile-time constant */
{
}

[SNIP]

The condition of the switch statement needs to be an integer expression (or
a class type with an existing single conversion function to integral type),
but the problem here is the case statement. For these the standard requires
an integral constant-expression.

Cheers
Chris
Jul 22 '05 #9

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

Similar topics

8
4438
by: David McDivitt | last post by:
We have a massive java application to be made ADA compliant. We want onfocus and onblur events for each text field. The best way seems to be javascript, by cycling through all the nodes recursively after page load, and attaching the events. We already cycle through everything to set tabs. If nodename is LABEL, we should be able to use the FOR...
4
2497
by: Hai Nguyen | last post by:
I'm learning C sharp and do not like vb much. I'm creatiing a wepage using panel to test myself. I tried to use these code below, which is written in VB, and to transform them to c sharp but I got hard time to understand vb syntax. I don't know if anyone in here can point out which is the equivalent object used in c sharp. Translate these...
4
2002
by: CaptRR | last post by:
I think this is the right group to post to, so here goes. My problem is this, I cannot update the datarow to save my life. Been on this for 2 days now, and still am no closer to figuring it out than I was before. I'm basicly taking date from some text boxes, trying to put them into a datarow and using that datarow to update the database,...
4
2355
by: Kiyomi | last post by:
Hello, I am trying to replace my alert message box with a popup page. In my page behind,
5
283
by: Seok Bee | last post by:
Hi All, I have a webform with a button to add record into the database. When the button is being clicked, the program will assign initial value controls in a detailsview control. When the first time, the button being click, the program works as expected. However, the next click on the button, the program showed the following error message:...
3
1265
by: RallyDSM | last post by:
Pre STory - I've had a lot of problems with this program, and I just added the last part of it (the add item code) and now an older part of the program crashes. Public Structure Stocks Public stockName As String Public numShares As Double Public datePurchased As String Public purchasePrice As Double Public currentPrice As Double
8
5341
by: =?Utf-8?B?R3JlZyBMYXJzZW4=?= | last post by:
I'm trying to figure out how to modify a panel (panel1) from a backgroundworker thread. But can't get the panel to show the new controls added by the backgroundwork task. Here is my code. In this code there is a panel panel1, that I populate with a lable in the foreground. Then when I click on "button1" a backgroundworker thread in async...
11
3949
by: Andrus | last post by:
I'm implementing entity object which should populate its properties from database when property is first referenced. In RDL reports I use object properties like MyObject.MyProperty MyObject is instance of MyEntity class. There is no MyProperty property in MyObject at design time.
4
4566
by: zion4ever | last post by:
Hello good people, Please bear with me as this is my first post and I am relative new to ASP. I do have VB6 experience. I have a form which enables users within our company to do an intranet reservation of available resources (laptops, beamers, etc). The MySql database queries are already in place, as is the ASP administration panel. The...
0
7703
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...
0
7618
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7926
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8138
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7679
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...
0
7983
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...
0
3657
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...
1
1228
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
946
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.