1

While browsing the code of my friend I came to notice this:

switch(State &state = getState()) {
case Begin: state = Search; break;
// other stuff similar
}

What is with the variable in the switch header? He is using GCC so I think this might be a GCC extension. Any idea?

Bill the Lizard
  • 369,957
  • 201
  • 546
  • 842
Johannes Schaub - litb
  • 466,055
  • 116
  • 851
  • 1,175

1 Answers1

9

It's not a secret or a GCC extension. Variables are allowed to be declared in the conditions of things like ifs, whiles, and switches. For example:

while (char c = cin.get()) { ... }

or

if (int* something = (int*)malloc(4)) { // but don't use malloc in C++
    // ...
}

After they are declared an initialised, they are converted to a bool value and if they evaluate to true the block is executed, and the block is skipped otherwise. Their scope is that of the construct whose condition they are declared in (and in the case of if, the scope is also over all the else if and else blocks too).

In §6.4.1 of the C++03 standard, it says

Selection statements choose one of several flows of control.

selection-statement:

    if ( condition ) statement
    if ( condition ) statement else statement
    switch ( condition ) statement

condition:

    expression
    type-specifier-seq declarator = assignment-expression

So as you can see, it allows type-specifier-seq declarator = assignment-expression in the condition of an if or switch. And you'd find something similar in the section for the "repetition constructs".

Also, switches work on integral or enum types or instances of classes that can be implicitly converted to an integral or enum type (§6.4.4):

The value of a condition that is an initialized declaration in a switch statement is the value of the declared variable if it has integral or enumeration type, or of that variable implicitly converted to integral or enumeration type otherwise.

I actually learned of this FROM AN ANSWER YOU POSTED on the "Hidden Features of C++" question. So I'm glad I could remind you of it :)

Community
  • 1
  • 1
Seth Carnegie
  • 70,115
  • 19
  • 169
  • 239
  • Yeah but it is on a `switch()`, which it means that it only accepts `int` values ? –  Apr 28 '12 at 17:38
  • @Muggen in the example `State` is an `enum` probably. – Seth Carnegie Apr 28 '12 at 17:39
  • @JohannesSchaub-litb no, but your lack of capitalisation indicates that you are asking a question to which you already know the answer :) – Seth Carnegie Apr 28 '12 at 17:39
  • 1
    @Seth, if it is not an enum, would behaviour like that be possible if the `operator int()` was overloaded ? –  Apr 28 '12 at 17:40
  • @Muggen yeah, apparently if it can be converted into an integral value then it will work too. – Seth Carnegie Apr 28 '12 at 17:42
  • @Muggen yeah, the standard says `The value of a condition that is an initialized declaration in a switch statement is the value of the declared variable if it has integral or enumeration type, or of that variable implicitly converted to integral or enumeration type otherwise.` – Seth Carnegie Apr 28 '12 at 17:50
  • How can you switch on a reference? – John Apr 28 '12 at 17:50
  • @John references are transparent; it's switching on a `State`. – Seth Carnegie Apr 28 '12 at 17:50
  • *"Variables are allowed to be declared in the conditions"* ... Damn it. – SigTerm Apr 28 '12 at 17:51