473,507 Members | 4,494 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 1703
* 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_endian()
{
int v = 1;

return 1 == * static_cast<char*>( static_cast<void*>( & 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_endian()
{
int v = 1;

return 1 == * static_cast<char*>( static_cast<void*>( & v ) );
******************
I'd've written:

unsigned int const i = 1;

return 1 == *reinterpret_cast<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******@mkwahler.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******@mkwahler.net> wrote in message
news:tF*****************@newsread1.news.pas.earthl ink.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
4432
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...
4
2488
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...
4
1999
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...
4
2351
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...
3
1263
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...
8
5337
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...
11
3944
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...
4
4560
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...
0
7105
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...
0
7308
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,...
0
7371
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...
0
7479
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...
1
5037
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...
0
3188
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...
0
3178
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
757
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
410
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...

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.