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

  • SEARCH
  • Home
  • 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 8225231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T15:20:54+00:00 2026-06-07T15:20:54+00:00

I am doing a switch statement over a char, and oftentimes the only difference

  • 0

I am doing a switch statement over a char, and oftentimes the only difference between, say, 'A' and 'a' is certain static constants i have in a particular struct and a random numeric constant. Here is an example:

switch(someChar)
{
case 'A':
{
    typedef structWithConstants<caseA, UPCASE> constantsT;
    someStruct s;
    s.bla = bla;
    s.foo = getfoo7(rat+constantsT::rat);
    s.other = getOther10(other + constantsT::other);

    someFunctionBar(&s);
}
break;
case 'a':
{
    typedef structWithConstants<caseA, LOWCASE> constantsT;
    someStruct s;
    s.bla = bla;
    s.foo = getfoo3(rat+constantsT::rat);
    s.other = getOther10(other + constantsT::other);

    someFunctionBar(&s);
}
break;
}

so in the above, literally the only difference in terms of code is the constantsT which gets used and the 7 switched to a three…is there a way to simplify the repetitive code above a bit? Maybe collapsing some of the common behavior between both cases?

  • 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-07T15:20:56+00:00Added an answer on June 7, 2026 at 3:20 pm

    it is possible to template a function, declare a type for those ‘foo’-s, and then pass the casing template param to some caller function

    typedef int getfootype(char c);
    
    template<getfootype f,char CASING>
    void  handle(char c)
    {
            typedef structWithConstants<caseA, CASING> constantsT;
            someStruct s;
            s.bla = bla;
            s.foo = f(rat+constantsT::rat);
            s.other = getOther10(other + constantsT::other);
    
            someFunctionBar(&s);
    }
    
    switch(someChar)
    {
        case 'A':
              handle<getfoo7,UPCASE>(someChar);
           break;
        case 'a':
              handle<getfoo3,LOWCASE>(someChar);
           break;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Doing some jquery animation. I have certain divs set up with an attribute of
Replace conditional with polymorphism is elegant only when type of object you're doing switch/if
I have a switch statement in my action script class.I have a function onKeyDown(event:
I have this switch statement in my code: switch(buttonIndex){ case 0: [actionSheet dismissWithClickedButtonIndex:buttonIndex animated:YES];
For as long as I can remember I have avoided using switch statement fall-through.
I am using a Java switch statement to assign different values to a variable
In an application that I have, I am doing quite frequent calls to Convert.ChangeType
So I have an issue with using a constant variable in the following switch
I am trying to change the value of my char array[10] within a switch
What's faster in PHP, making a large switch statement, or setting up an array

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.