1136

I've always wondered this - why can't you declare variables after a case label in a switch statement? In C++ you can declare variables pretty much anywhere (and declaring them close to first use is obviously a good thing) but the following still won't work:

switch (val)  
{  
case VAL:  
  // This won't work
  int newVal = 42;  
  break;
case ANOTHER_VAL:  
  ...
  break;
}  

The above gives me the following error (MSC):

initialization of 'newVal' is skipped by 'case' label

This seems to be a limitation in other languages too. Why is this such a problem?

4
  • 11
    For an explanation based on the C BNF grammar, see stackoverflow.com/questions/1180550/weird-switch-error-in-obj-c/…
    – johne
    Commented Jan 12, 2010 at 3:30
  • Here is a really good read about switch statements and labels (ABC:) in general. Commented Aug 16, 2012 at 22:33
  • 6
    I would say 'Why can't variables be initialized in a switch statement rather than declared'.Since just declaring the variable give me only a warning in MSVC.
    – ZoomIn
    Commented Dec 9, 2013 at 6:21
  • 4
    If you put everything inside the case label within curly braces { } then it will work.
    – E Purdy
    Commented Aug 27, 2020 at 13:33

23 Answers 23

1355

Case statements are only labels. This means the compiler will interpret this as a jump directly to the label. In C++, the problem here is one of scope. Your curly brackets define the scope as everything inside the switch statement. This means that you are left with a scope where a jump will be performed further into the code skipping the initialization.

The correct way to handle this is to define a scope specific to that case statement and define your variable within it:

switch (val)
{   
case VAL:  
{
  // This will work
  int newVal = 42;  
  break;
}
case ANOTHER_VAL:  
...
break;
}
8
  • 4
    @TallJef I don't know what 'old days' you're referring to. I have never encountered a compiler where all the stack space for a method isn't allocated when the method is entered, in 40 years.
    – user207421
    Commented Apr 10, 2017 at 8:05
  • @EJP: Well, when _alloca() is used, the compiler can't know how much space is needed at entry, so it has to make piecemeal adjustments.
    – Ben Voigt
    Commented Nov 29, 2017 at 21:29
  • 2
    I encountered a peculiar situation with such statemenet in IAR compiler. There was an array inside case (with scope), but memory was allocated regardless of entering the case, just by entering function. Since other cases led to deeper stack than this one, it eventually resulted in a stack overflow.
    – Do-do-new
    Commented Apr 30, 2018 at 11:40
  • @MarquisofLorne I definitely have. Actually relied to it in some app where I had a recursive function with a temporary array that wasn't allocated through the whole function call, and not when a recursive call was made.
    – gnasher729
    Commented Jun 10, 2020 at 4:38
  • 1
    @StephenEckels: Please no. The IIFE is not idiomatic C++ and hiding it in a macro is even worse. All programmer expectations of what break;, return etc do are violated.
    – Ben Voigt
    Commented Jan 2, 2023 at 15:35
466

This question was originally tagged as and at the same time. The original code is indeed invalid in both C and C++, but for completely different unrelated reasons.

  • In C++ this code is invalid because the case ANOTHER_VAL: label jumps into the scope of variable newVal bypassing its initialization. Jumps that bypass initialization of automatic objects are illegal in C++. This side of the issue is correctly addressed by most answers.

  • However, in C language bypassing variable initialization is not an error. Jumping into the scope of a variable over its initialization is legal in C. It simply means that the variable is left uninitialized. The original code does not compile in C for a completely different reason. Label case VAL: in the original code is attached to the declaration of variable newVal. In C language declarations are not statements. They cannot be labeled. And this is what causes the error when this code is interpreted as C code.

      switch (val)  
      {  
      case VAL:             /* <- C error is here */
        int newVal = 42;  
        break;
      case ANOTHER_VAL:     /* <- C++ error is here */
        ...
        break;
      }
    

    Adding an extra {} block fixes both C++ and C problems, even though these problems happen to be very different. On the C++ side it restricts the scope of newVal, making sure that case ANOTHER_VAL: no longer jumps into that scope, which eliminates the C++ issue. On the C side that extra {} introduces a compound statement, thus making the case VAL: label to apply to a statement, which eliminates the C issue.

  • In C case the problem can be easily solved without the {}. Just add an empty statement after the case VAL: label and the code will become valid

      switch (val)  
      {  
      case VAL:;            /* Now it works in C! */
        int newVal = 42;  
        break;
      case ANOTHER_VAL:  
        ...
        break;
      }
    

    Note that even though it is now valid from C point of view, it remains invalid from C++ point of view.

  • Symmetrically, in C++ case the the problem can be easily solved without the {}. Just remove the initializer from variable declaration and the code will become valid

      switch (val)  
      {  
      case VAL: 
        int newVal;
        newVal = 42;  
        break;
      case ANOTHER_VAL:     /* Now it works in C++! */
        ...
        break;
      }
    

    Note that even though it is now valid from C++ point of view, it remains invalid from C point of view.

Starting from C23 all labels in C language will be interpreted as labelling implied null statements (N2508), i.e. the issue with being unable to place labels in front of declarations in C will no longer exist and the above ;-based fix will no longer be necessary.

9
  • 7
    @AnT: I understand why the one that fixes C++ isn't applicable for C; however, I'm unable to understand how it fixes the C++ issue of skipping the initialization in the first place? Wouldn't it still skip the declaration and assignment of newVal when it jumps to ANOTHER_VAL?
    – legends2k
    Commented Jun 19, 2015 at 9:08
  • 23
    @legends2k: Yes, it still skips it. However, when I say "it fixes the issue" , I mean that it fixes the C++ compiler error. In C++ it is illegal to skip a scalar declaration with initializer, but it is perfectly fine to skip a scalar declaration without initializer. At case ANOTHER_VAL: point variable newVal is visible, but with indeterminate value. Commented Jun 19, 2015 at 14:45
  • 4
    Fascinating. I found this question after reading §A9.3: Compound Statement from K&R C (second edition). The entry mentioned the technical definition of a compound-statement which is {declaration-list[opt] statement-list[opt]}. Confused, because I had thought a declaration WAS a statement, I looked it up and immediately found this question, an example where said disparity becomes apparent and actually breaks a program. I believe another solution (for C) would be to put another statement (possibly a null statement?) before the declaration so that the labeled-statement is satisfied. Commented Feb 4, 2017 at 10:04
  • 1
    Oops, I just noticed that the null-statement solution I suggested is already in your answer. Never mind, then. Commented Feb 4, 2017 at 10:19
  • 7
    It's worth noting that the fix of adding an empty statement only works for C99 onwards. In C89, variables must be declared at the start of their enclosing block. Commented Dec 12, 2019 at 16:40
147

Ok. Just to clarify this strictly has nothing to do with the declaration. It relates only to "jumping over the initialization" (ISO C++ '03 6.7/3)

A lot of the posts here have mentioned that jumping over the declaration may result in the variable "not being declared". This is not true. An POD object can be declared without an initializer but it will have an indeterminate value. For example:

switch (i)
{
   case 0:
     int j; // 'j' has indeterminate value
     j = 0; // 'j' set (not initialized) to 0, but this statement
            // is jumped when 'i == 1'
     break;
   case 1:
     ++j;   // 'j' is in scope here - but it has an indeterminate value
     break;
}

Where the object is a non-POD or aggregate the compiler implicitly adds an initializer, and so it is not possible to jump over such a declaration:

class A {
public:
  A ();
};

switch (i)  // Error - jumping over initialization of 'A'
{
   case 0:
     A j;   // Compiler implicitly calls default constructor
     break;
   case 1:
     break;
}

This limitation is not limited to the switch statement. It is also an error to use 'goto' to jump over an initialization:

goto LABEL;    // Error jumping over initialization
int j = 0; 
LABEL:
  ;

A bit of trivia is that this is a difference between C++ and C. In C, it is not an error to jump over the initialization.

As others have mentioned, the solution is to add a nested block so that the lifetime of the variable is limited to the individual case label.

4
  • 2
    "Error jumping over initialization"??? Not with my GCC. It may give a "j may be used unitialized" warning when using j below label, but there is no error. However, in case of switch, there is an error (a hard error, not a weak warning).
    – Mecki
    Commented Feb 20, 2010 at 1:20
  • 10
    @Mecki: It is illegal in C++. ISO C++ '03 - 6.7/3: "...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 (3.9) and is declared without an initializer (8.5)." Commented Feb 22, 2010 at 18:21
  • 1
    Yes, but it is not illegal in C (at least gcc says it's not). j will be uninitialized (have some random number), but the compiler compiles it. However, in case of the switch statement, the compiler won't even compile it and I fail to see the difference between a goto/label case and a switch case.
    – Mecki
    Commented Feb 22, 2010 at 22:01
  • 9
    @Mecki: In general a single compiler behaviour does not necessarily reflect whtat is actually allowed by the language. I've checked both C'90 and C'99 and both standards include an example with a jump over initialization in a switch statement. Commented Feb 28, 2010 at 22:23
45

The whole switch statement is in the same scope. To get around it, do this:

switch (val)
{
    case VAL:
    {
        // This **will** work
        int newVal = 42;
    }
    break;

    case ANOTHER_VAL:
      ...
    break;
}

Note the brackets.

0
37

After reading all answers and some more research I get a few things.

Case statements are only 'labels'

In C, according to the specification,

§6.8.1 Labeled Statements:

labeled-statement:
    identifier : statement
    case constant-expression : statement
    default : statement

In C there isn't any clause that allows for a "labeled declaration". It's just not part of the language.

So

case 1: int x=10;
        printf(" x is %d",x);
break;

This will not compile, see http://codepad.org/YiyLQTYw. GCC is giving an error:

label can only be a part of statement and declaration is not a statement

Even

  case 1: int x;
          x=10;
            printf(" x is %d",x);
    break;

this is also not compiling, see http://codepad.org/BXnRD3bu. Here I am also getting the same error.


In C++, according to the specification,

labeled-declaration is allowed but labeled -initialization is not allowed.

See http://codepad.org/ZmQ0IyDG.


Solution to such condition is two

  1. Either use new scope using {}

    case 1:
           {
               int x=10;
               printf(" x is %d", x);
           }
    break;
    
  2. Or use dummy statement with label

    case 1: ;
               int x=10;
               printf(" x is %d",x);
    break;
    
  3. Declare the variable before switch() and initialize it with different values in case statement if it fulfills your requirement

    main()
    {
        int x;   // Declare before
        switch(a)
        {
        case 1: x=10;
            break;
    
        case 2: x=20;
            break;
        }
    }
    

Some more things with switch statement

Never write any statements in the switch which are not part of any label, because they will never executed:

switch(a)
{
    printf("This will never print"); // This will never executed

    case 1:
        printf(" 1");
        break;

    default:
        break;
}

See http://codepad.org/PA1quYX3.

2
  • 2
    You correctly described the C issue. But the assertion that in C++ labelled initialization is not allowed is completely not true. There's nothing wrong with labeled initialization in C++. What C++ does not allow is jumping over initialization of variable a into the scope of variable a. So, from C point of view, the problems is with case VAL: label and you described it correctly. But from C++ point of view, the problem is with case ANOTHER_VAL: label. Commented Nov 7, 2013 at 8:14
  • In C++, unlike in C, declarations are a subset of statements. Commented Aug 7, 2016 at 0:01
21

You can't do this, because case labels are actually just entry points into the containing block.

This is most clearly illustrated by Duff's device. Here's some code from Wikipedia:

strcpy(char *to, char *from, size_t count) {
    int n = (count + 7) / 8;
    switch (count % 8) {
    case 0: do { *to = *from++;
    case 7:      *to = *from++;
    case 6:      *to = *from++;
    case 5:      *to = *from++;
    case 4:      *to = *from++;
    case 3:      *to = *from++;
    case 2:      *to = *from++;
    case 1:      *to = *from++;
               } while (--n > 0);
    }
}

Notice how the case labels totally ignore the block boundaries. Yes, this is evil. But this is why your code example doesn't work. Jumping to a case label is the same as using goto, so you aren't allowed to jump over a local variable with a constructor.

As several other posters have indicated, you need to put in a block of your own:

switch (...) {
    case FOO: {
        MyObject x(...);
        ...
        break; 
    }
    ...
 }
9
  • 1
    This Duff's device implementation has a bug that makes it extremely slow: count is type int so the % must perform a real division/modulo operation. Make count unsigned (or better yet, always use size_t for counts/indices) and the problem goes away. Commented Jul 3, 2010 at 20:58
  • 1
    @R..: What?! In a two's complement system, signedness doesn't affect modulos by powers of 2 (it's just an AND on the bottom bits), and doesn't affect divisions by powers of 2 as long as your processor architecture has an arithmetic right-shift operation (SAR in x86, versus SHR which is for unsigned shifts). Commented Sep 2, 2010 at 14:00
  • @Chris: I believe he means when the compiler must allow for negative values where "just an AND on the bottom bits" doesn't hold; for example, -1 % 8 gives -1 on this two's complement system using g++ (the sign in this case is implementation defined per 5.6/4).
    – Roger Pate
    Commented Sep 2, 2010 at 15:45
  • 3
    @Chris: I agree with you that R is exaggerating the impact; I only saw your comment and knew a simple AND didn't suffice.
    – Roger Pate
    Commented Sep 2, 2010 at 23:21
  • 1
    Also worth noting is the original Wikipedia code is for sending data to a memory mapped output, which looks odd here because it's not mentioned and every byte is copied to the same "to" location. Could get around that by either adding postfix ++ to the to, or mentioning the use case is for memory mapped IO. Totally peripheral to the original question :-).
    – Peter
    Commented Mar 4, 2011 at 23:47
16

Most of the replies so far are wrong in one respect: you can declare variables after the case statement, but you can't initialize them:

case 1:
    int x; // Works
    int y = 0; // Error, initialization is skipped by case
    break;
case 2:
    ...

As previously mentioned, a nice way around this is to use braces to create a scope for your case.

3
  • 1
    Mr. 32 you have misunderstood what your error is: yes that's not going to compile but not because you're declaring a variable inside a switch. The error is because you're trying to declare a variable after a statement, which is illegal in C. Commented Dec 18, 2011 at 15:26
  • 1
    Now a days that is legal in c90 and newer version of c Commented Dec 19, 2011 at 4:39
  • Yes, that's correct. I came across this because I (Eclipse user) was wondering that CLion (I'm experimenting with it) shows red error marks with text "Use of undeclared identifier '<variable>' ", while the build ends without errors. This is apparently related to clang settings. Either way, the compiler - gcc - does compile without errors & warnings (contrary to MS C). Even initializing a variable yields no build errors. Old answers above should be edited because effectively outdated.
    – Rob
    Commented Mar 7 at 9:32
13

My favorite evil switch trick is to use an if(0) to skip over an unwanted case label.

switch(val)
{
case 0:
// Do something
if (0) {
case 1:
// Do something else
}
case 2:
// Do something in all cases
}

But very evil.

3
  • Very nice. Example of why: case 0 and case 1 might for instance initializing a variable differently that is then used in case 2.
    – hlovdal
    Commented Mar 19, 2009 at 10:02
  • 1
    If you want both case 0 and case 1 to fall through case 2. ( without case 0 falling through case 1 ). Don't know if it's really useful, but sure works.
    – Petruza
    Commented Dec 18, 2011 at 6:55
  • 1
    You can just jump to the required label with goto without obfuscation of the code Commented Sep 24, 2016 at 19:12
10

Try this:

switch (val)
{
    case VAL:
    {
        int newVal = 42;
    }
    break;
}
7

Consider:

switch(val)
{
case VAL:
   int newVal = 42;
default:
   int newVal = 23;
}

In the absence of break statements, sometimes newVal gets declared twice, and you don't know whether it does until runtime. My guess is that the limitation is because of this kind of confusion. What would the scope of newVal be? Convention would dictate that it would be the whole of the switch block (between the braces).

I'm no C++ programmer, but in C:

switch(val) {
    int x;
    case VAL:
        x=1;
}

Works fine. Declaring a variable inside a switch block is fine. Declaring after a case guard is not.

2
  • 3
    @Mr.32 : actually your example shows that a printf is not executed, but in this case, the int x is not a statement but a declaration, the x is declared, space for it is reserved every time the function environment gets stacked, see: codepad.org/4E9Zuz1e
    – Petruza
    Commented Dec 18, 2011 at 7:03
  • I was expecting to find this when reading the title of the question, because the question is not about declaring variables within "case:" labels, but in switch statements. And only you (and VictorH, emphasising your answer) actually talked about variables in switch statements.
    – cesss
    Commented Jan 3, 2018 at 22:47
7

You can declare variables within a switch statement if you start a new block:

switch (thing)
{ 
  case A:
  {
    int i = 0;  // Completely legal
  }
  break;
}

The reason is to do with allocating (and reclaiming) space on the stack for storage of the local variable(s).

1
  • 1
    The variable can be declared, but it cannot be initialized. Also, I'm pretty sure that the issue does not relate in anyway to the stack and local variables. Commented Sep 18, 2008 at 14:12
4

The entire section of the switch is a single declaration context. You can't declare a variable in a case statement like that. Try this instead:

switch (val)  
{  
case VAL:
{
  // This will work
  int newVal = 42;
  break;
}
case ANOTHER_VAL:  
  ...
  break;
}
2
  • The variable can be declared, but it cannot be initialized. Commented Sep 18, 2008 at 14:11
  • @Richard Corden I am confident that initialization will work. Do you still assert it cannot be initialized? Commented Sep 13, 2013 at 19:07
3

If your code says "int newVal=42" then you would reasonably expect that newVal is never uninitialised. But if you goto over this statement (which is what you're doing) then that's exactly what happens - newVal is in-scope but has not been assigned.

If that is what you really meant to happen then the language requires to make it explicit by saying "int newVal; newVal = 42;". Otherwise you can limit the scope of newVal to the single case, which is more likely what you wanted.

It may clarify things if you consider the same example but with "const int newVal = 42;"

3

I just wanted to emphasize slim's point. A switch construct creates a whole, first-class-citizen scope. So it is posible to declare (and initialize) a variable in a switch statement before the first case label, without an additional bracket pair:

switch (val) {  
  /* This *will* work, even in C89 */
  int newVal = 42;  
case VAL:
  newVal = 1984; 
  break;
case ANOTHER_VAL:  
  newVal = 2001;
  break;
}
2
  • -1 here int newVal = 42; wil be never executed. see this codepad.org/PA1quYX3 Commented Dec 18, 2011 at 5:55
  • 4
    the declaration int newVal will be executed, but not the = 42 assignment.
    – Petruza
    Commented Dec 18, 2011 at 7:10
3

So far the answers have been for C++.

For C++, you can't jump over an initialization. You can in C. However, in C, a declaration is not a statement, and case labels have to be followed by statements.

So, valid (but ugly) C, invalid C++

switch (something)
{
  case 1:; // Ugly hack empty statement
    int i = 6;
    do_stuff_with_i(i);
    break;
  case 2:
    do_something();
    break;
  default:
    get_a_life();
}

Conversly, in C++, a declaration is a statement, so the following is valid C++, invalid C

switch (something)
{
  case 1:
    do_something();
    break;
  case 2:
    int i = 12;
    do_something_else();
}
1
  • 1
    The second example is NOT valid C++(test with vc2010 and gcc 4.6.1 C++ not allow skip the initialization part. gcc error message is : cross initialization of 'int i'
    – zhaorufei
    Commented Dec 26, 2011 at 9:50
3

Interesting that this is fine:

switch (i)  
{  
case 0:  
    int j;  
    j = 7;  
    break;  

case 1:  
    break;
}

... but this isn't:

switch (i)  
{  
case 0:  
    int j = 7;  
    break;  

case 1:  
    break;
}

I get that a fix is simple enough, but I'm not understanding yet why the first example doesn't bother the compiler. As was mentioned earlier (2 years earlier hehe), declaration is not what causes the error, even despite the logic. Initialisation is the problem. If the variable is initialised and declared on the different lines, it compiles.

1
  • 1
    First is not fine on gcc 4.2: "error: expected expression before 'int'". As Peter and Mr.32 say, "case 0: ; int j; ..." and "case 0: ; int j = 7; ..." do both work. The problem in C is just that "case <label>: declaration" is not valid C syntax.
    – dubiousjim
    Commented May 30, 2012 at 21:21
3

I wrote this answer orginally for this question. However when I finished it I found that answer has been closed. So I posted it here, maybe someone who likes references to standard will find it helpful.

Original Code in question:

int i;
i = 2;
switch(i)
{
    case 1: 
        int k;
        break;
    case 2:
        k = 1;
        cout<<k<<endl;
        break;
}

There are actually 2 questions:

1. Why can I declare a variable after case label?

It's because in C++ label has to be in form:

N3337 6.1/1

labeled-statement:

...

  • attribute-specifier-seqopt case constant-expression : statement

...

And in C++ declaration statement is also considered as statement (as opposed to C):

N3337 6/1:

statement:

...

declaration-statement

...

2. Why can I jump over variable declaration and then use it?

Because: N3337 6.7/3

It is possible to transfer into a block, but not in a way that bypasses declarations with initialization. A program that jumps (The transfer from the condition of a switch statement to a case label is considered a jump in this respect.)

from a point where a variable with automatic storage duration is not in scope to a point where it is in scope is ill-formed unless the variable has scalar type, class type with a trivial default constructor and a trivial destructor, a cv-qualified version of one of these types, or an array of one of the preceding types and is declared without an initializer (8.5).

Since k is of scalar type, and is not initialized at point of declaration jumping over it's declaration is possible. This is semantically equivalent:

goto label;

int x;

label:
cout << x << endl;

However that wouldn't be possible, if x was initialized at point of declaration:

 goto label;

    int x = 58; //error, jumping over declaration with initialization

    label:
    cout << x << endl;
2

A switch block isn't the same as a succession of if/else if blocks. I'm surprised no other answer explains it clearly.

Consider this switch statement :

switch (value) {
    case 1:
        int a = 10;
        break;
    case 2:
        int a = 20;
        break;
}

It may be surprising, but the compiler will not see it as a simple if/else if. It will produce the following code :

if (value == 1)
    goto label_1;
else if (value == 2)
    goto label_2;
else
    goto label_end;

{
label_1:
    int a = 10;
    goto label_end;
label_2:
    int a = 20; // Already declared !
    goto label_end;
}

label_end:
    // The code after the switch block

The case statements are converted into labels and then called with goto. The brackets create a new scope and it is easy to see now why you can't declare two variables with the same name within a switch block.

It may look weird, but it is necessary to support fallthrough (that is, not using break to let execution continue to the next case).

0

New variables can be decalared only at block scope. You need to write something like this:

case VAL:  
  // This will work
  {
  int newVal = 42;  
  }
  break;

Of course, newVal only has scope within the braces...

Cheers, Ralph

0

C++ Standard has: It is possible to transfer into a block, but not in a way that bypasses declarations with initialization. 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 (3.9) and is declared without an initializer (8.5).

The code to illustrate this rule:

#include <iostream>

using namespace std;

class X {
  public:
    X() 
    {
     cout << "constructor" << endl;
    }
    ~X() 
    {
     cout << "destructor" << endl;
    }
};

template <class type>
void ill_formed()
{
  goto lx;
ly:
  type a;
lx:
  goto ly;
}

template <class type>
void ok()
{
ly:
  type a;
lx:
  goto ly;
}

void test_class()
{
  ok<X>();
  // compile error
  ill_formed<X>();
}

void test_scalar() 
{
  ok<int>();
  ill_formed<int>();
}

int main(int argc, const char *argv[]) 
{
  return 0;
}

The code to show the initializer effect:

#include <iostream>

using namespace std;

int test1()
{
  int i = 0;
  // There jumps fo "case 1" and "case 2"
  switch(i) {
    case 1:
      // Compile error because of the initializer
      int r = 1; 
      break;
    case 2:
      break;
  };
}

void test2()
{
  int i = 2;
  switch(i) {
    case 1:
      int r;
      r= 1; 
      break;
    case 2:
      cout << "r: " << r << endl;
      break;
  };
}

int main(int argc, const char *argv[]) 
{
  test1();
  test2();
  return 0;
}
0

It appears that anonymous objects can be declared or created in a switch case statement for the reason that they cannot be referenced and as such cannot fall through to the next case. Consider this example compiles on GCC 4.5.3 and Visual Studio 2008 (might be a compliance issue tho' so experts please weigh in)

#include <cstdlib>

struct Foo{};

int main()
{
    int i = 42;

    switch( i )
    {
    case 42:
        Foo();  // Apparently valid
        break;

    default:
        break;
    }
    return EXIT_SUCCESS;
}
3
  • If you're going to vote it down please explain why. I'm curious to know why creating an anonymous object appears to be an exemption.
    – Olumide
    Commented May 31, 2016 at 14:03
  • 1
    not a DV, but: The whole question is about declaring/scope of named variables. A temporary ("anonymous object" isn't a term) isn't a named variable, nor is it a declaration, nor is it subject to scope (unless bound to a const reference with scope of its own). It's an expression that lives and dies within its statement (wherever that may be). Therefore, it's wholly irrelevant. Commented Jul 17, 2016 at 17:55
  • 1
    Foo(); is not a declaration; the question is about declarations.
    – M.M
    Commented Jul 6, 2017 at 4:02
-1

I believe the issue at hand is that is the statement was skipped, and you tried to use the var elsewhere, it wouldn't be declared.

-1

newVal exists in the entire scope of the switch but is only initialised if the VAL limb is hit. If you create a block around the code in VAL it should be OK.

Not the answer you're looking for? Browse other questions tagged or ask your own question.