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 3337850
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T00:18:10+00:00 2026-05-18T00:18:10+00:00

I have a bug in my current code that I have been banging my

  • 0

I have a bug in my current code that I have been banging my head against for a couple days. I’ll post the summary version of the code below (not my actual code, but it still compiles).

#include <iostream>

using namespace std;

int main()
{
  int x = 7;

  switch(x)
  {
    case 1:
    {
      case 2:
      cout << "hi";
    }
  }
}

I like to sometimes introduce a tighter scope to my switch cases so local variable names don’t interfere with ones I’ve used earlier. Apparently I had a phone call to answer or something one day and didn’t finish writing the case statement as I wished, but later on was certain that I had. case 2 should have actually been in a nested switch, not as part of the original outter switch (for those wondering, I use named constants in my code, not just magic numbers). On g++, I didn’t get a warning or error when compling without any options.

My question: why allow cross-scope case jumps from a switch? What purpose does it serve?

  • 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-05-18T00:18:10+00:00Added an answer on May 18, 2026 at 12:18 am

    Duff’s Device is hardly the “purpose” of cross-scope case labels as already suggested, it is merely an exploitation of it.

    The truth is probably that it serves no intentionally designed purpose, but is merely an artefact of the most simple possible implementation that works. The design is such that the switch-case construct can do exactly what is reasonable, but does not explicitly protect you from the unreasonable either.

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

Sidebar

Related Questions

I have a bug in my code that made me think I don't fully
i have bug that i cannot find, i have Class Point with method who
I have a Bug at my simple REGEX. I have been trying to write
We have a bug in our application that does not occur every time and
We have this bug that only appears 30% of the time for the Release
I have this code, and for some reason my $.post function fires twice in
I have following code to get different parts of current system Date (10-11-2011 for
I have noticed that when I was running Windows XP, if my code dereferenced
I have been developing a library that is getting pretty large, and now I
Yet another PHP date question for everyone - I have the current code for

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.