compilation error in switch case in c++

R

Rahul

Hi Everyone,

I have the following program,

class A
{
int i;
};

int main()
{
switch(1)
{
case 1 :
// {
A obj;
printf("case 1\n");
// }
break;
case 2 :
A obj1;
printf("case 2\n");
break;
}
}


and when i compile, i get an compilation error, saying,

jump to case symbol
enters scope of non-POD 'A obj'

but if i remove the commented open and close brace, it works just
fine, could anyone clarify the exact reason of the compiler reporting
the error,

Thanks in advance!!!
 
G

Gianni Mariani

Rahul said:
Hi Everyone,

I have the following program,

class A
{
int i;
};

int main()
{
switch(1)
{
case 1 :
// {
A obj;
printf("case 1\n");
// }
break;
case 2 :
A obj1;
printf("case 2\n");
break;
}
}


and when i compile, i get an compilation error, saying,

jump to case symbol
enters scope of non-POD 'A obj'

but if i remove the commented open and close brace, it works just
fine, could anyone clarify the exact reason of the compiler reporting
the error,

These are the errors I get with gcc when I compile your code - I made a
few changes i.e. made it a read non POD class.

clcpp_t6.cpp: In function `int main()':
clcpp_t6.cpp:20: error: jump to case label
clcpp_t6.cpp:16: error: crosses initialization of `A obj'

The errors should be self explanatory.

#include <cstdio>

class A
{
public:
int i;
A() : i() {}
};

int main()
{
switch(1)
{
case 1 :
// {
A obj;
std::printf("case 1\n");
// }
break;
case 2 :
A obj1;
std::printf("case 2\n");
break;
}
}
 
P

Pete Becker

class A
{
int i;
};

int main()
{
switch(1)
{
case 1 :
// {
A obj;
printf("case 1\n");
// }
break;
case 2 :
A obj1;
printf("case 2\n");
break;
}
}

This is illegal. When should obj's destructor be run?
 
J

James Kanze

On 2008-02-06 04:15:14 -0500, Rahul <[email protected]> said:
This is illegal. When should obj's destructor be run?

When you leave scope. There's no problem with the destructor
(formally, at least); you're allowed to jump out of scope, and
it's up to the compiler to call any destructors. (And of
course, the semantics of switch/break are those of goto.)

The problem is rather when will the initialization occur. In
the above example, without the {}, obj is in scope, visible and
usable in case 2. The code jumps over an initialization. (And
I don't think that this exact example is illegal---class A
doesn't have any initialization. But it would be illegal if
class A had a non-trivial constructor, or if obj had any
initialization expression.)

The reason why the declaration of obj1 is legal, of course, is
that there isn't any label after it. It is the label ("case
2:") after the declaration which causes the code to be illegal.

Of course, the reason why you're not allowed to jump over
non-trivial initialization is because doing anything with the
object afterwards is very problematic. And in C++, you will do
something with the object when it goes out of scope: call the
destructor. (Presumably, if the object has trivial
initialization, the destructor knows how to cope with it, and
calling the destructor without having done the initialization is
OK. In practice, I don't think I've ever seen an object with a
non-trivial destructor which didn't have non-trivial
constructors as well.)
 
P

Pete Becker

When you leave scope. There's no problem with the destructor
(formally, at least);

On the contrary: in some cases, the destructor should be run, and in
others it should not. Please note that some responses are not intended
to convey the exact formal details of the C++ standard, but to give a
sense of the reasons behind the rules.
 
T

Triple-DES

This is illegal. When should obj's destructor be run?

I fail to see how this is illegal, even if the brackets are not there:

A program that jumps from a point where a local variable with
automatic storage duration is not in scope to a point where it is in
scope is ill-formed __unless__ the variable has POD-type and is
declared without an initializer. (6.7/3, emphasis mine)

If A turned out to be non-POD it would be illegal and the compiler
error would be correct.
 
J

James Kanze

I fail to see how this is illegal, even if the brackets are not there:
A program that jumps from a point where a local variable with
automatic storage duration is not in scope to a point where it is in
scope is ill-formed __unless__ the variable has POD-type and is
declared without an initializer. (6.7/3, emphasis mine)
If A turned out to be non-POD it would be illegal and the compiler
error would be correct.

In the original code, A was a non POD type (it has a private
data member), which is why it was illegal.
 

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,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top