Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7858077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T21:15:50+00:00 2026-06-02T21:15:50+00:00

While browsing the code of my friend I came to notice this: switch(State &state

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T21:15:52+00:00Added an answer on June 2, 2026 at 9:15 pm

    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 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While browsing some source code I came across a function like this: void someFunction(char
While browsing some code, I came across this line: if False: #shedskin I understand
I came across this code while I was browsing for html5 canvas examples. I
While browsing I came across this blog post about using the Wikipedia API from
I came across this presentation while browsing SO some time ago, and it relates
I am using this IP Validation Function that I came across while browsing, it
I was browsing some code and I came across this macro definition #define D(x)
While browsing the code of some websites I sometimes notice that some JavaScript files
While browsing the code of an erlang application, I came across an interesting design
While browsing questions and answers in this forum i found a piece of code

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.