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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T09:07:56+00:00 2026-05-16T09:07:56+00:00

Today while writing some Visual C++ code I have come across something which has

  • 0

Today while writing some Visual C++ code I have come across something which has surprised me. It seems C++ supports ++ (increment) for bool, but not — (decrement). It this just a random decision, or there is some reason behind this?

This compiles:

static HMODULE hMod = NULL;
static bool once = false;
if (!once++)
    hMod = LoadLibrary("xxx");

This does not:

static HMODULE hMod = NULL;
static bool once = true;
if (once--)
    hMod = LoadLibrary("xxx");
  • 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-16T09:07:57+00:00Added an answer on May 16, 2026 at 9:07 am

    It comes from the history of using integer values as booleans.

    If x is an int, but I am using it as a boolean as per if(x)... then incrementing will mean that whatever its truth value before the operation, it will have a truth-value of true after it (barring overflow).

    However, it’s impossible to predict the result of -- given knowledge only of the truth value of x, as it could result in false (if the integral value is 1) or true (if the integral value is anything else – notably this includes 0 [false] and 2 or more [true]).

    So as a short-hand ++ worked, and -- didn’t.

    ++ is allowed on bools for compatibility with this, but its use is deprecated in the standard and it was removed in C++17.


    This assumes that I only use x as an boolean, meaning that overflow can’t happen until I’ve done ++ often enough to cause an overflow on it’s own. Even with char as the type used and CHAR_BITS something low like 5, that’s 32 times before this doesn’t work any more (that’s still argument enough for it being a bad practice, I’m not defending the practice, just explaining why it works) for a 32-bit int we of course would have to use ++ 2^32 times before this is an issue. With -- though it will only result in false if I started with a value of 1 for true, or started with 0 and used ++ precisely once before.

    This is different if we start with a value that is just a few below 0. Indeed, in such a case we might want ++ to result in the false value eventually such as in:

    int x = -5;
    while(++x)
      doSomething(x);
    

    However, this example treats x as an int everywhere except the conditional, so it’s equivalent to:

    int x = -5;
    while(++x != 0)
      doSomething(x);
    

    Which is different to only using x as a boolean.

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

Sidebar

Related Questions

Today while I was writing some code for two methods that shows and hides
While cleaning some code today written by someone else, I changed the access modifier
Today I was writing some C code to sort an array of structs using
I have been writing java for a while, and today I encountered the following
I stumbled upon this oddity today while playing with some code to go down
Today while i was coding , I encounter something which I didn't expect it
I realized today while writing some Python that one could write the inequality operator
I came across Side-by-side Assemblies for the first time today while trying to install
While searching a bug in my code today I found a strange thing. When
I'm applying UnitTest just for a while, and today I met something very strange.

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.