Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are all the possible values of a bool value in C++?

Tags:

c++

c++11

The question is not so obvious as it seems, and I have trouble finding much information about the bool type in the standard.

According to the C++11 standard, what are the guarantees associated with the bool type with regards to:

  • Storage: how much space does it take, ignoring alignment? Is there any requirement for the value that will be stored to represent trueand false?
  • Values taken: Let b be of type bool, does the assertion (b == true) || (b == false) hold? Is (false < true) well-formed, and does it hold?
like image 863
Laurent LA RIZZA Avatar asked Sep 24 '13 14:09

Laurent LA RIZZA


People also ask

What are the possible values for a bool value?

It has two possible values: True and False , which are special versions of 1 and 0 respectively and behave as such in arithmetic contexts.

Is bool always 0 or 1?

Boolean values and operations C++ is different from Java in that type bool is actually equivalent to type int. Constant true is 1 and constant false is 0. It is considered good practice, though, to write true and false in your program for boolean values rather than 1 and 0.


2 Answers

bool types are described in section §3.9.1, Fundamental types. Of relevance here is a sentence from paragraph 6:

Values of type bool are either true or false.47

The reference footnote 47 provides some interesting additional information:

47) Using a bool value in ways described by this International Standard as “undefined,” such as by examining the value of an uninitialized automatic object, might cause it to behave as if it is neither true nor false.

This is just a direct consequence of the standard imposing no requirements on programs with undefined behaviour.

There are no size requirements on bool, other than the implicit "at least one byte" that applies to all types as a consequence of the C++ memory model.

There are also no requirements on the internal representation of bool objects, however, due to the requirements regarding integral conversions (true must convert to 1 and false to 0), implementations may be inclined to pick the same representations for true and 1, and for false and 0, since that makes such conversions unnecessary.

like image 157
R. Martinho Fernandes Avatar answered Oct 02 '22 03:10

R. Martinho Fernandes


Storage: how much space does it take, ignoring alignment?

Implementation defined, but in practice one byte. It can't usually be smaller, since that's the smallest possible object size. Exceptions are:

  • bitfield class members can be a single bit;
  • std::vector<bool> packs values so that each takes a single bit; but doesn't really hold objects of type bool. Other types (like std::bitset) do similar things, but don't pretend to be storing bool.

Is there any requirement for the value that will be stored to represent true and false?

No; just the requirement that, when converted to a numeric type, true becomes 1 and false becomes 0. In practice that means that an implementation is likely to use those values; although, on some platforms, other representations might work better.

Values taken: Let b be an object of type bool, does the assertion (b == true) || (b == false) hold?

The assertion will hold if b has been initialised or assigned with a valid value. If it's uninitialised, then it may not hold; but you have undefined behaviour anyway, if you use an uninitialised value. In fact, the standard contains a specific footnote (referenced by C++11 3.9.1/6) warning about this:

47) Using a bool value in ways described by this International Standard as “undefined,” such as by examining the value of an uninitialized automatic object, might cause it to behave as if it is neither true nor false.

UPDATE: the question keeps on growing:

Is (false < true) well-formed, and does it hold?

Yes, and yes. The operands are promoted to int, giving 0 < 1, which is true.

like image 40
Mike Seymour Avatar answered Oct 02 '22 01:10

Mike Seymour