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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:42:32+00:00 2026-05-27T04:42:32+00:00

The question is simple. Why does this compile: bool b(true); if (b) { /*

  • 0

The question is simple. Why does this compile:

bool b(true);
if (b) { /* */ }

And this compile:

if (bool b = true) { /* */ }

But not this:

if (bool b(true)) { /* */ }

In my real code, I need to construct an object and test it, while also having it destroyed when the if-block ends. Basically, I’m looking for something like this:

{
    Dingus dingus(another_dingus);
    if (dingus) {
        // ...
    }
}

Of course, this would work:

if (Dingus dingus = another_dingus) { /* */ }

But then I’m constructing a Dingus and calling operator= on it. It seems logical to me that I would be able to construct the object using whatever constructor I please.

But I’m baffled why this isn’t grammatically correct. I’ve tested with G++ and MSVC++ and they both complain about this construct, so I’m sure it’s part of the spec but I’m curious as to the reasoning for this and what non-ugly workarounds there may be.

  • 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-27T04:42:33+00:00Added an answer on May 27, 2026 at 4:42 am

    It’s a bit technical. There’s no reason why what you want couldn’t be allowed, it just isn’t. It’s the grammar.

    An if statement is a selection statement, and it takes the grammatical form:

    if (condition) statement
    

    Here, condition can be either:

    • expression or
    • type-specifier-seq declarator = assignment-expression

    And there you have it. Allowing a declaration in a condition is a special case, and it must follow that form or your program is ill-formed. They could have probably allow direct-initialization instead of copy-initialization, but there isn’t really any motivation to do so now. As Johannes Schaub points out, this change would break existing code, so it’s pretty much never going to happen.

    Let_Me_Be notes that C++11 added a third form (I’m ignoring attributes here):

    decl-specifier-seq declarator braced-init-list
    

    So if (bool b{true}) is fine. (This can’t possibly break any valid existing code.)


    Note your question seems to do with efficiency: don’t worry. The compiler will elide the temporary value and just construct the left-hand side directly. This, however, requires your type be copyable (or movable in C++11).

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

Sidebar

Related Questions

I am aware of this question , but it does not appear to work
Very simple question of where does this code work? static void *gostruct[] = {
Simple question - what does this actually do? var oq = (ObjectQuery<TEntity>)L2EQuery; return ExecuteFirstorDefault<TEntity>(oq,
I'm embarrassed to ask such a simple question. My term does not start for
A simple question, but could someone provide sample code as to how would someone
There is a really similar question here , but the provided answer does not
I actually posted this question to pjsip mailing list, but did not get any
This may be a too broad question but how would you develop a simple
This question is for anyone that's had personal experience. The question is simple: Does
Very simple question. What does the term 'seeding' mean in general? I'll put the

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.