Can't use object as a case label!

J

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

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
 
A

Alf P. Steinbach

* 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.
 
J

JKop

Alf P. Steinbach posted:
* JKop:

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
 
G

Gianni Mariani

JKop said:
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.
 
M

Mike Wahler

JKop said:
Alf P. Steinbach posted:


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
 
J

JKop

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;

}

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
 
J

JKop

'Blah' is not an integer expression.

Yeah but I was hoping that "operator int() const" would do
its job...


-JKop
 
C

Chris Theis

Mike Wahler said:
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
 

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,787
Messages
2,569,630
Members
45,338
Latest member
41Pearline46

Latest Threads

Top