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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:27:55+00:00 2026-05-25T19:27:55+00:00

According to the standard, what is the difference in behavior between declaring variables in

  • 0

According to the standard, what is the difference in behavior between declaring variables in control structures versus declaring variables elsewhere? I can’t seem to find any mention of it.

If what I’m referring to isn’t clear, here’s an example:

if (std::shared_ptr<Object> obj = objWeakPtr.lock())

As you can see, I’m declaring and initializing a local variable, obj, in the if block.

Also, is there any technical reason as to why this syntax isn’t given any special behavior when used in place of a conditional? For example, adding an additional set of brackets results in a compiler error; this also prevents the variable from being chained with other conditions.

// Extra brackets, won't compile.
if ((std::shared_ptr<Object> obj = objWeakPtr.lock()))

// If the above were valid, something like this could be desirable.
if ((std::shared_ptr<Object> obj = objWeakPtr.lock()) && obj->someCondition())
  • 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-25T19:27:55+00:00Added an answer on May 25, 2026 at 7:27 pm

    According to the standard, what is the difference in behavior between declaring variables in control structures versus declaring variables elsewhere? I can’t seem to find any mention of it.

    Declarations inside control structure introductions are no different that declarations elsewhere. That’s why you can’t find any differences.

    6.4/3 does describe some specific semantics for this, but there are no surprises:

    [n3290: 6.4/3]: A name introduced by a declaration in a condition
    (either introduced by the type-specifier-seq or the declarator of the
    condition) is in scope from its point of declaration until the end of
    the substatements controlled by the condition. If the name is
    re-declared in the outermost block of a substatement controlled by the
    condition, the declaration that re-declares the name is ill-formed. [..]


    Also, is there any technical reason as to why this syntax isn’t given any special behavior when used in place of a conditional? For example, adding an additional set of brackets results in a compiler error; this also prevents the variable from being chained with other conditions.

    An if condition can contain either a declarative statement or an expression. No expression may contain a declarative statement, so you can’t mix them either.

    [n3290: 6.4/1]: Selection statements choose one of several flows of control.

    selection-statement:
        if ( condition ) statement
        if ( condition ) statement else statement
        switch ( condition ) statement
    condition:
        expression
        attribute-specifier-seq[opt] decl-specifier-seq declarator = initializer-clause
        attribute-specifier-seq[opt] decl-specifier-seq declarator braced-init-list
    

    It all just follows from the grammar productions.

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

Sidebar

Related Questions

I seem to struggling with what I thought was standard functionality of bundler, according
According to the standard, a conversion function has a function-id operator conversion-type-id , which
according to the HTML Standards ID-Attributes of any HTML Tag in a webpage have
According to select name from system_privilege_map System has been granted: SELECT ANY TABLE ...and
According to what I have found so far, I can use the following code:
I know that according to C++ standard in case the new fails to allocate
In C99, equality == does not seem ever to be undefined. It can produce
What is the difference between main in a C program and in a C++
Possible Duplicate: What's the difference between Array(1) and new Array(1) in JavaScript? In javascript,
According to the CSS 2.1 standard, the vertical margins of floating elements should not

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.