PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > compilation error in switch case in c++
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
compilation error in switch case in c++

Réponse
 
LinkBack Outils de la discussion
Vieux 06/02/2008, 09h15   #1
Rahul
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut compilation error in switch case in c++

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!!!


  Réponse avec citation
Vieux 06/02/2008, 10h37   #2
Gianni Mariani
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: compilation error in switch case in c++

Rahul wrote:
> 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;
}
}
  Réponse avec citation
Vieux 06/02/2008, 13h07   #3
Pete Becker
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: compilation error in switch case in c++

On 2008-02-06 04:15:14 -0500, Rahul <sam_cit@yahoo.co.in> said:

>
> 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?

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

  Réponse avec citation
Vieux 07/02/2008, 09h46   #4
James Kanze
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: compilation error in switch case in c++

On Feb 6, 2:07 pm, Pete Becker <p...@versatilecoding.com> wrote:
> On 2008-02-06 04:15:14 -0500, Rahul <sam_...@yahoo.co.in> said:


> > 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?


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.)

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
  Réponse avec citation
Vieux 07/02/2008, 12h07   #5
Pete Becker
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: compilation error in switch case in c++

On 2008-02-07 04:46:30 -0500, James Kanze <james.kanze@gmail.com> 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);


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.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

  Réponse avec citation
Vieux 08/02/2008, 07h10   #6
Triple-DES
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: compilation error in switch case in c++

On 6 Feb, 14:07, Pete Becker <p...@versatilecoding.com> wrote:
> On 2008-02-06 04:15:14 -0500, Rahul <sam_...@yahoo.co.in> said:
> > 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?
>


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)

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


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


  Réponse avec citation
Vieux 08/02/2008, 10h48   #7
James Kanze
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: compilation error in switch case in c++

On Feb 8, 8:10 am, Triple-DES <fire13...@hotmail.com> wrote:
> On 6 Feb, 14:07, Pete Becker <p...@versatilecoding.com> wrote:


> > On 2008-02-06 04:15:14 -0500, Rahul <sam_...@yahoo.co.in> said:
> > > 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?


> 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)


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


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

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 10h57.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,17510 seconds with 15 queries