Arundale Ramanathan
Arundale Ramanathan

Reputation: 2069

is bool datatype portable in c++?

Can bool datatype be used in C++ and can portability still be ensured?

There are discussions about this partially in other posts, but does not specifically discuss portability.

I would like to know if I can use bool and be sure it will compile in most systems (say 99%), if not all systems.

Upvotes: 0

Views: 540

Answers (4)

Matteo Italia
Matteo Italia

Reputation: 126777

bool is a builtin type since the first C++ standard (C++98), so if you have a compiler that conforms to any C++ standard (and arguably, any compiler from the last 20 years) you should be set.

As for pre-standard C++, looking around it seems that, on PC compilers, it made its appearance around 1996-1997 (Visual C++ 5, Borland C++ 5); indeed, GotW #26 mentions that bool is "the only builtin datatype to be added to C++ since [Stroutroup's] Annotated C++ Reference Manual" (1990), so it's reasonable to deduce that it was born somewhere between 1990 and 1996. Reading around the web 1993 is thrown around quite often as "birth date" for bool in C++, but I couldn't find a single reliable reference for it.

Although you can still find obsolete pages (mostly from university courses) that explain how to workaround the absence of bool in old compilers, nowadays there are no compatibility issues using it, unless you are forced to use really ancient toolchains (I'm looking at you, university courses which recommend Turbo C++ in 2018).

Maybe you got confused with C, where bool appeared only in C99?


Was bool available in pre-standard C++ (like Cfront)?

Looking at the sources of Cfront 3.0.3 (1994, although the original 3.0 was released in 1991), bool is thrown around quite a bit indeed, especially in standard headers such as basic_string.h.

As for its definition, there is an insane mix of typedef char bool; (but inside an #ifdef CFRONT_COMPATIBLE_LUCENT, so maybe it's just a compatibility definition? And WTF, the copyright date is 1996?), #define bool char, enum bool { false, true };, as well as a typedef int bool; in a promisingly-named std_bool.h header (which however has copyright date 1996 as well!).

Looking at the lexer and the parser I couldn't find mentions of bool, although the demangler cites it as a fundamental type, so there is some evidence that somebody thought it was (or, was going to be) a builtin.

Still, while its precise definition is a bit of a blur, as said above something named bool with true and false values was indeed available and used in the standard library, so, if you plan to compile your code with Cfront 3.0, bool is probably going to be the least of your concerns. :-)

Incidentally, this trip down ancient C++ brings some sadness as well: Cfront 3.0 shipped with a C++ regex library, something that we had to wait for C++11 to get back again (and which remained segfault-level broken for quite some more years in libstdc++).

Upvotes: 5

SoronelHaetir
SoronelHaetir

Reputation: 15164

Note that if you are worried about storage portability (serialization) then bool is not a particularly portable type, the size is not tied down (indeed it is hardly ever the same size as 'char').

This despite the fact that bool has been in c++ as long as the language has been standardized.

Upvotes: 1

C++ does not mean much (it is more a family of related languages, than a single one). You need to target a specific C++ standard. Consider at least C++11 (but don't bother about older standards, which probably mentioned bool also), and if possible C++14.

Then bool is part of C++11 (and also of later standards like C++14 or C++17). Check by reading n3337. It was also part of C++98 but you should not care.

(Notice that C++ has changed a lot since C++11 included, this is why I don't recommend using older standards like C++98; and portability matters only when referring to a particular standard; it usually does not make sense to write code portable to all of C++98, C++11, C++17, C++20)

If you want to code for an obsolete standard like C++98 something which could compile with a later standard-conforming compiler (e.g. some C++17 compatible one) you should ask a different question. bool is then not an issue, but you have many other (more important) ones.

The question of what is the C++ standard supported on most platforms is a very different one. If you can afford using recent compilers (e.g. if you can install recent versions of GCC or Clang) I would bet on C++11.

You'll certainly be able to find, perhaps in some museum, an obsolete computer which has a C++98 compiler but not a C++11 one. You could even find computers without C++ compilers (or even without any compilers).

Certainly, an old Sun3/60 from 1987 in a museum accepts a C++ dialect (perhaps some Cfront) very different of the several dialects of C++ accepted by GCC 8 on my Linux desktop.

What you want to code against is a standard (such as C++11, which, like C++98, C++14, C++17 also mentions bool). This ensures portability to other implementations of the same standard. Of course C++98, C++11, C++14, C++17 all mention bool; but if you want to code against several C++ standards (and this could be difficult) you need a lot of care (probably using some preprocessor conditionals to provide different code for different C++ standards when that is needed).

Upvotes: -4

YePhIcK
YePhIcK

Reputation: 5856

bool is a built-in type so it is as portable as the C++ language itself

Upvotes: 8

Related Questions