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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T17:55:41+00:00 2026-06-05T17:55:41+00:00

We know that if we try to access a nonexistent key of std::map with

  • 0

We know that if we try to access a nonexistent key of std::map with the operator [] , the function will insert a new element with that key.

We have: std::map<std::string, bool> map_xxx;

Is it guaranteed that after accessing the nonexistent key of map_xxx["nonexistent_key"], the value of the second argument will always be false ?

ps. if no, any ideas how to have this behavior?

  • 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-05T17:55:42+00:00Added an answer on June 5, 2026 at 5:55 pm

    Yes. The value to be inserted is guaranteed to be false.


    In C++98, the mechanism was called default initialization, specified as zero initialization for non-classes; that’s false for Booleans.

    Since C++03, the mechanism is called value initialization, still specified as zero initialization for non-classes; and thus still false for Booleans. For example, let’s see what C++14 has to say on this.

    From §23.4.4.3; just substitute bool for “T”.

    T& operator[](const key_type& x);

    1. Effects: If there is no key equivalent to x in the map, inserts value_type(x, T()) into the map.
    2. Requires: key_type shall be CopyInsertable and mapped_type shall be DefaultInsertable into
      *this.

    From §8.5, digest the paragraphs from the bottom up:

    To zero-initialize an object or reference of type T means:

    — if T is a scalar type (3.9), the object is initialized to the value obtained by converting the integer literal 0 (zero) to T;

    …

    To value-initialize an object of type T means:

    — if T is a (possibly cv-qualified) class type (Clause 9) with either no default constructor (12.1) or a default constructor that is user-provided or deleted, then the object is default-initialized;

    — if T is a (possibly cv-qualified) class type without a user-provided or deleted default constructor, then the object is zero-initialized and the semantic constraints for default-initialization are checked, and if T has a non-trivial default constructor, the object is default-initialized;

    — if T is an array type, then each element is value-initialized;

    — otherwise, the object is zero-initialized.

    …

    An object whose initializer is an empty set of parentheses, i.e., (), shall be value-initialized.

    From §4.12:

    A prvalue of arithmetic, unscoped enumeration, pointer, or pointer to member type can be converted to a prvalue of type bool. A zero value, null pointer value, or null member pointer value is converted to false; any other value is converted to true. For direct-initialization (8.5), a prvalue of type std::nullptr_t can be converted to a prvalue of type bool; the resulting value is false.

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

Sidebar

Related Questions

I try to access a specific registry key that i know exist. I use
I do have somewhat of a feeling that people will try to beat me
How can I know during runtime that my code threw a Warning? example try
I know that exceptions have a performance penalty, and that it's generally more efficient
We know that behind the scenes, the ASP.NET MVC framework will use reflection to
Now I know that I should not try to reinvent the wheel and try
I know that somewhere in the history of the internet this must have been
I know that a good unit test should never access the file system. So
I have a bug in my 404 setup. I know that because, when I
Also I know that anything that I create using alloc I have to release

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.