Topic: Why no auto variables inside a switch?
Author: Jim Hyslop <jim.hyslop@leitch.com>
Date: 2000/08/21 Raw View
In article
<Pine.OSF.4.10.10008140941210.14192-100000@cassius.its.unimelb.edu.au>,
Richard John Cavell <rjcavell@student.unimelb.edu.au> wrote:
[snip]
> I need the object code to get out
> of the function as fast as possible, and I don't want to create space
> for
> a bunch of variables that won't be used 99% of the time it enters the
> loop.
At the risk of venturing into compiler-specific territory here, I
wouldn't worry about the *space* being used. AFAIK, VC only reserves
space for local variables once, upon entry to the function. If, on the
other hand, the variables being created have non-trivial constructors,
then you might want to worry about it.
Creating a local scope (as others have suggested) is your best bet, IMO.
--
Jim
This message was posted using plain text only. Any hyperlinks you may
see were added other parties without my permission.
I do not endorse any products or services that may be hyperlinked to
this message.
Sent via Deja.com http://www.deja.com/
Before you buy.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Balog Pal (mh)" <pasa@lib.hu>
Date: 2000/08/23 Raw View
Richard John Cavell wrote in message ...
>switch (variable)
>{
>case 1: return 1;
>case 2: int tempvar = MyFunc();
> return tempvar;
>}
>The compiler (Visual C++) chucks a wobbly about the declaration of a
>variable inside a switch statement. Is my code above non-standard?
consider the case like a set of labels, with one at the very end too, and a
set of goto-s at the beginning. As it is forbidden to goto over a variable
initialisation, you get an error message. Even if you would not, I suggest
to use something like this on a reglar basis for cases:
switch(var)
{
case 1:
{
// code
}
break;
case 2:
case 3:
{
// code
}
break;
default:
{
// code
}
break;
}
Redundant breaks will not hurt anyone. The {} part systematically prevents
crosstalk, seeing possible locals introduced on branches. (that also removes
the error above). If you need to fall through, comment out the break, and
write: fallthrough.
That way you can avoid a plenty of headache.
Paul
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Richard John Cavell <rjcavell@student.unimelb.edu.au>
Date: 2000/08/14 Raw View
Hi all,
If I do this :
switch (variable)
{
case 1: return 1;
case 2: int tempvar = MyFunc();
return tempvar;
}
The compiler (Visual C++) chucks a wobbly about the declaration of a
variable inside a switch statement. Is my code above non-standard?
(Yes, I know I could eliminate the variable, I'm actually just posting a
simple example of a complex problem). I need the object code to get out
of the function as fast as possible, and I don't want to create space for
a bunch of variables that won't be used 99% of the time it enters the
loop.
--------------
Richard Cavell
Melbourne University Medical Student, Debater, Chess Player, etc.
- r.cavell@ugrad.unimelb.edu.au
Celebrating the Micro$oft breakup.
Newsgroups - Please keep it on the group, and copy your replies to me via
email. (Server problems).
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Dennis Yelle <dennis51@jps.net>
Date: 2000/08/14 Raw View
Richard John Cavell wrote:
>
> Hi all,
>
> If I do this :
>
> switch (variable)
> {
> case 1: return 1;
> case 2: int tempvar = MyFunc();
> return tempvar;
> }
>
> The compiler (Visual C++) chucks a wobbly about the declaration of a
> variable inside a switch statement.
I think your compiler will like it if you write it this way:
switch (variable)
{
case 1: return 1;
case 2:
{
int tempvar = MyFunc();
return tempvar;
}
}
I do this all the time to get around that problem in Visual C++.
Dennis Yelle
--
I am a computer programmer and I am looking for a job.
There is a link to my resume here:
http://www.fortunecity.com/roswell/barada/186/index.html
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Richard Parkin" <rparkin@msi-eu.com>
Date: 2000/08/14 Raw View
"Richard John Cavell" <rjcavell@student.unimelb.edu.au> wrote in message
news:Pine.OSF.4.10.10008140941210.14192-100000@cassius.its.unimelb.edu.au...
> Hi all,
>
> If I do this :
>
> switch (variable)
> {
> case 1: return 1;
> case 2: int tempvar = MyFunc();
> return tempvar;
> }
>
> The compiler (Visual C++) chucks a wobbly about the declaration of a
> variable inside a switch statement. Is my code above non-standard?
Yes. From 6.7/3, it even mentions switch in a footnote.
The only variables are non-initialised POD types.
To see why consider
int i=2;
switch( i )
{
case 1: MyClassWithDestructor oops();
case 2: std::cout << "Is Oops constructed?";
}
std::cout << "And did it get destructed?";
Ric
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Ron Natalie <ron@sensor.com>
Date: 2000/08/14 Raw View
Richard John Cavell wrote:
>
> switch (variable)
> {
> case 1: return 1;
> case 2: int tempvar = MyFunc();
> return tempvar;
> }
>
> The compiler (Visual C++) chucks a wobbly about the declaration of a
> variable inside a switch statement. Is my code above non-standard?
>
It's not the declaration, it's the initialization. C++ doesn't allow you
to transfer control into the scope of a variable after where it is initialized
(control must pass through the initialization.
The trivial fix is to make the scope of the variable emcompass only that choice
in the switch statement with { }
switch(var) {
case 1: return l;
case 2: {
int tempvar = MyFunc();
return tempvar;
}
}
I'd suspect the original is legal, there's really no way to bypass the init of
tempvar. It becomes illegal when you put an additional case after case 2 because
tempvar would still be in scope there.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Reico GmbH <Entwicklung@reico.de>
Date: Tue, 15 Aug 2000 01:27:02 GMT Raw View
Hi,
the solution is:
...
case 2:
{
int tempvar = MyFunc();
return tempvar;
}
AFAIK implicit auto variables inside a case/break are forbidden, because
you are able to fall through to the next case:
int i = 0;
switch (number) {
case 1:
int i = something;
if (i == 0)
break;
case 2:
do_what (i); // which i ?
break;
}
Hth
Olaf Krzikalla
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: llewelly.@@edevnull.dot.com
Date: 2000/08/15 Raw View
Richard John Cavell <rjcavell@student.unimelb.edu.au> writes:
> Hi all,
>
> If I do this :
>
> switch (variable)
> {
> case 1: return 1;
> case 2: int tempvar = MyFunc();
> return tempvar;
> }
Try this:
switch (variable)
{
case 1: return 1;
case 2:
{
int tempvar = MyFunc();
return tempvar;
}
}
[snip]
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Michiel Salters <salters@lucent.com>
Date: 2000/08/15 Raw View
Richard Parkin wrote:
[ in response to a question which isn't related to my note ]
> To see why consider
> int i=2;
> switch( i )
> {
> case 1: MyClassWithDestructor oops();
> case 2: std::cout << "Is Oops constructed?";
> }
> std::cout << "And did it get destructed?";
> Ric
To answer your questions: There is never a class constructed, no matter
what i would be (assuming it was legal to start with).
When you wrote MyClassWithDestructor oops(); you declared a function named
oops, taking no parameters and returning a MyClassWithDestructor.
oops is thus neither constructed nor destructed, but declared.
Oops indeed.
Michiel Salters
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Richard Parkin" <rparkin@msi-eu.com>
Date: 2000/08/15 Raw View
"Michiel Salters" <salters@lucent.com> wrote in message
news:3998E600.D075AB55@lucent.com...
> Richard Parkin wrote:
>
> [ in response to a question which isn't related to my note ]
>
> > To see why consider
>
> > int i=2;
> > switch( i )
> > {
> > case 1: MyClassWithDestructor oops();
> > case 2: std::cout << "Is Oops constructed?";
> > }
> > std::cout << "And did it get destructed?";
>
>
> To answer your questions: There is never a class constructed, no matter
> what i would be (assuming it was legal to start with).
>
> When you wrote MyClassWithDestructor oops(); you declared a function named
> oops, taking no parameters and returning a MyClassWithDestructor.
> oops is thus neither constructed nor destructed, but declared.
Well spotted.
This is one of my common typos, so I'm not surprised it slipped past. I
think it comes from automatically writing the brackets ready to add any
parameters, but if there aren't any, I sometimes forget to remove them.
Even though I know the 'choose function declaration' rule 8.5/8, I still
find it annoyingly hard to remember (or easy to forget). Although not as
annoying as trying to work out the bizarre error messages that get generated
from it.
Ric
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: David R Tribble <david@tribble.com>
Date: 2000/08/18 Raw View
Richard Parkin wrote:
>> [ in response to a question which isn't related to my note ]
>>
>> To see why consider
>>
>> int i=2;
>> switch( i )
>> {
>> case 1: MyClassWithDestructor oops(); <--
>> case 2: std::cout << "Is Oops constructed?";
>> }
>> std::cout << "And did it get destructed?";
"Michiel Salters" <salters@lucent.com> wrote
>> When you wrote MyClassWithDestructor oops(); you declared a function
>> named oops, taking no parameters and returning a
>> MyClassWithDestructor.
>> oops is thus neither constructed nor destructed, but declared.
Richard Parkin wrote:
> Well spotted.
>
> This is one of my common typos, so I'm not surprised it slipped past.
> I think it comes from automatically writing the brackets ready to add
> any parameters, but if there aren't any, I sometimes forget to remove
> them.
>
> Even though I know the 'choose function declaration' rule 8.5/8, I
> still find it annoyingly hard to remember (or easy to forget).
> Although not as annoying as trying to work out the bizarre error
> messages that get generated from it.
If you prefer to always use the constructed-style syntax for
variable declarations, you could get in the habit of always providing
a constructor taking an unnamed int parameter, so that you could
then always provide a dummy '0' argument to the otherwise empty
variable declaration parameter list:
class Foo
{
public:
Foo();
Foo(int); // Added for convenience
...
};
void bar()
{
Foo f(0); // Variable def, not a func decl
...
}
--
David R. Tribble, mailto:david@tribble.com, http://david.tribble.com
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]