Topic: C++ Coding Standards (question)


Author: df@fox.NoSubdomain.NoDomain (ASTS David Fout)
Date: 9 May 1994 19:05:49 GMT
Raw View
Is it required to block the scope of a CASE module?  The following
code compiles with the SU C++ 3.0 compiler, but not on the native
Concurrent C++ compiler that is bundled with RTU 6.1A.

extern "C" int printf(const char* format, ...);

main()
{
    int i;
    i=1;
    switch (i)
    {
        case 1:
        int i;
            i=100;
            printf("i = %d\n", i);
            break ;
        default:
       ;
    }
    printf("i = %d\n", i);
}

The Concurrent compiler complains that the CASE module must be blocked
with curly braces because of the type declaration of "i" in the case
module.  I guess the real question is, do case statements have there own
scope without using curly braces.

This will work on both compilers:

extern "C" int printf(const char* format, ...);

main()
{
    int i;
    i=1;
    switch (i)
    {
        case 1:
 {
        int i;
            i=100;
            printf("i = %d\n", i);
            break ;
        }
        default:
       ;
    }
    printf("i = %d\n", i);


Thanks.
}






--
David G. Fout                          Phone:    (301) 953-3330
Century Computing                      Internet: dfout@cen.com




Author: kanze@us-es.sel.de (James Kanze)
Date: 10 May 1994 13:34:10 GMT
Raw View
In article <2qm1id$b1g@paperboy.gsfc.nasa.gov>
df@fox.NoSubdomain.NoDomain (ASTS David Fout) writes:

|> Is it required to block the scope of a CASE module?  The following
|> code compiles with the SU C++ 3.0 compiler, but not on the native
|> Concurrent C++ compiler that is bundled with RTU 6.1A.

|> extern "C" int printf(const char* format, ...);

|> main()
|> {
|>     int i;
|>     i=1;
|>     switch (i)
|>     {
|>         case 1:
|>         int i;
|>             i=100;
|>             printf("i = %d\n", i);
|>             break ;
|>         default:
|>        ;
|>     }
|>     printf("i = %d\n", i);
|> }

|> The Concurrent compiler complains that the CASE module must be blocked
|> with curly braces because of the type declaration of "i" in the case
|> module.  I guess the real question is, do case statements have there own
|> scope without using curly braces.

Case statements do not have scope, and in general, it is a good idea
to put the code for each case in its own curly braces.

Nevertheless, unless you have made an error when copying the code, the
above code should work.  Problems should only occur if the declared
object has an initializer; your `int i' doesn't.
--
James Kanze                       email: kanze@lts.sel.alcatel.de
GABI Software, Sarl., 8 rue du Faisan, F-67000 Strasbourg, France
Conseils en informatique industrielle --
                   -- Beratung in industrieller Datenverarbeitung




Author: pete@genghis.interbase.borland.com (Pete Becker)
Date: Thu, 12 May 1994 02:59:41 GMT
Raw View
In article <CpnxuA.2EA@venus.csci.csc.com>,
Tim Ottinger <tottinge@butch.csci.csc.com> wrote:
>
>Actually, there is a rule on this.  You can't jump past the point of
>declaration of a variable to another point where the variable is still
>in scope.  See Section 6.4.2 in the ARM.  To hit the default, you jump
>past the declaration of i, but are within its scope.
>
>This is illegal, unless you include the curly braces around the body
>of the first case.
>

 Well, my copy of the ARM has the following text in section 6.4.2:

 It is illegal ... to jump past a declaration WITH AN EXPLICIT OR
 IMPLICIT INITIALIZER unless the declaration is in an inner block
 that is not entered.... [emphasis added]

 The emphasized words are crucial. The integer variable in the
original posting did not have an explicit or implicit initializer, so the
jump past its declaration was not illegal under this clause.

 void f( int i )
 {
 switch(i)
  {
  case 1:
      int j;
      j = 3;
      break;
  case 2:
      j = 4;
      break;
  }
 }

 This code is legal, although not particularly good style.
 -- Pete





Author: tottinge@butch.csci.csc.com (Tim Ottinger)
Date: Thu, 12 May 1994 00:10:58 GMT
Raw View
In article <KANZE.94May10143410@slsvhdt.us-es.sel.de>, kanze@us-es.sel.de (James Kanze) writes:
> In article <2qm1id$b1g@paperboy.gsfc.nasa.gov>
> df@fox.NoSubdomain.NoDomain (ASTS David Fout) writes:
[clipped]
> |> extern "C" int printf(const char* format, ...);
>
> |> main()
> |> {
> |>     int i;
> |>     i=1;
> |>     switch (i)
> |>     {
> |>         case 1:
> |>         int i;
> |>             i=100;
> |>             printf("i = %d\n", i);
> |>             break ;
> |>         default:
> |>        ;
> |>     }
> |>     printf("i = %d\n", i);
> |> }
[ Clipped]
> Case statements do not have scope, and in general, it is a good idea
> to put the code for each case in its own curly braces.
>
> Nevertheless, unless you have made an error when copying the code, the
> above code should work.  Problems should only occur if the declared
> object has an initializer; your `int i' doesn't.

Actually, there is a rule on this.  You can't jump past the point of
declaration of a variable to another point where the variable is still
in scope.  See Section 6.4.2 in the ARM.  To hit the default, you jump
past the declaration of i, but are within its scope.

This is illegal, unless you include the curly braces around the body
of the first case.

Tim.
--
--------------------------------------------------------
I speak only for myself, but you may feel free to agree.