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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:59:25+00:00 2026-06-14T00:59:25+00:00

I commonly have code where I want to detect some condition, and if it

  • 0

I commonly have code where I want to detect some condition, and if it exists, then process that condition.

The test for the condition extracts certain information that is important to the processing half of the problem.

However, if/else constructs fail to allow one to declare local-to-the-if variables in the if construct itself(1). Hence, I have to put variable declarations before the beginning of the entire if/else chain for all of the various conditions for which information may be extracted.

For example:

    else if ((EndsWithToken("d") || EndsWithToken("dsh")) &&
             (p1 = find(m_input_line, '[')) &&
             (p2 = find(m_input_line, ']')) &&
              p2 > p1)
    {
                // process what we found inside of the [ ] brackets using p1 and p2...
            }

This snippet is part of a long chain of if/else’s, and the others don’t need p1 and p2 – but they might need an integer value that was extracted from the input stream, or a double, or two points, or whatever….

The point is, there is no way that I know of introduce const TCHAR * p1 and p2 locally to the above scope, the only place it is used, that still allows for the overall structure to remain the same.

Is there some C++ language feature or a pattern that might allow for this?


(1) As some have pointed out, it is possible to declare a single variable of fundamental type, if it is the fist thing, and to test it against non-zero only. This is far too limited to be of much use as a general solution to this issue (though the occasional moment could come up where it is useful – but truly rare).

  • 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-14T00:59:26+00:00Added an answer on June 14, 2026 at 12:59 am

    One solution would be to make each case a distinct function, that returns whether or not it succeeded.

    This gives you a natural well-localized scope for the variables, and hopefully (assuming) a reasonable compiler can inline them.

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

Sidebar

Related Questions

All, Say you have some code like below. I want to know If It
At work we have a very large code-base that we commonly export for a
In the STL library some containers have iterators and it is commonly held that
I have a bunch of different objects that are commonly edited in the same
I have two tables, that share the same definition. I commonly insert new objects
In some code I have converted to SSE I preform some ray tracing, tracing
I have some exe files that I've uploaded into my database as I don't
I'm writing some code where, most commonly, no results will be returned from a
I am working on some code that parses IL byte arrays as returned by
I have a code that does something like this: char16_t msg[256]={0}; //... wstring wstr;

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.