There are two kinds of implementations defined by the C++ standard:hosted andfreestanding implementations. Forhosted implementations, the set of standard library headers required by the C++ standard is much larger than forfreestanding ones. In afreestanding implementation, execution may happen without an operating system.
The kind of the implementation is implementation-defined. The predefined macro__STDC_HOSTED__
is expanded to1 for hosted implementations and0 for freestanding implementations.(since C++11)
Requirements onmulti-threaded executions and data races
| (since C++11) |
freestanding | hosted |
---|---|
In afreestanding implementation, it is implementation-defined whether a program is required to define amain function. Start-up and termination is implementation-defined; start-up contains the execution ofconstructors for objects ofnamespace scope with static storage duration; termination contains the execution ofdestructors for objects with staticstorage duration. | In ahosted implementation, a program must contain a global function calledmain. Executing a program starts a mainthread of execution in which themain function is invoked, and in which variables of staticstorage duration might be initialized and destroyed. |
Afreestanding implementation has an implementation-defined set of headers. This set includes at least the headers in the following table.
For partially freestanding headers, freestanding implementations only needs to provide part of the entities in the corresponding synopsis:
| (since C++26) |
Library | Component | Headers | Freestanding |
---|---|---|---|
Language support | Common definitions | <cstddef> | All |
C standard library | <cstdlib> | Partial | |
Implementation properties | <cfloat> <climits>(since C++11) <limits> <version>(since C++20) | All | |
Integer types | <cstdint>(since C++11) | All | |
Dynamic memory management | <new> | All | |
Type identification | <typeinfo> | All | |
Source location | <source_location>(since C++20) | All | |
Exception handling | <exception> | All | |
Initializer lists | <initializer_list>(since C++11) | All | |
Comparisons | <compare>(since C++20) | All | |
Coroutines support | <coroutine>(since C++20) | All | |
Other runtime support | <cstdarg> | All | |
Debugging support | <debugging>(since C++26) | All | |
Concepts | <concepts>(since C++20) | All | |
Diagnostics | Error numbers | <cerrno>(since C++26) | Partial |
System error support | <system_error>(since C++26) | Partial | |
Memory management | Memory | <memory>(since C++23) | Partial |
Metaprogramming | Type traits | <type_traits>(since C++11) | All |
Compile-time rational arithmetic | <ratio>(since C++23) | All | |
General utilities | Utility components | <utility>(since C++23) | All |
Tuples | <tuple>(since C++23) | All | |
Function objects | <functional>(since C++20) | Partial | |
Primitive numeric conversions | <charconv>(since C++26) | Partial | |
Bit manipulation | <bit>(since C++20) | All | |
Strings | String classes | <string>(since C++26) | Partial |
Null-terminated sequence utilities | <cstring>(since C++26) | Partial | |
Text processing | Null-terminated sequence utilities | <cwchar>(since C++26) | Partial |
Iterators | <iterator>(since C++23) | Partial | |
Ranges | <ranges>(since C++23) | Partial | |
Numerics | Mathematical functions for floating-point types | <cmath>(since C++26) | Partial |
Random number generation | <random>(since C++26) | Partial | |
Concurrency support | Atomics | <atomic>(since C++11) | All[1] |
Execution control | <execution>(since C++26) | Partial | |
Deprecated headers | <ciso646>(until C++20) <cstdalign>(since C++11)(until C++20) <cstdbool>(since C++11)(until C++20) | All |
Some compiler vendors may not fully support freestanding implementation. For example, GCC libstdc++ has had implementation and build issues before version 13, while LLVM libcxx and MSVC STL do not support freestanding.
In C++23, many features are made freestanding with partial headers. However, it is still up for discussion in WG21 whether some headers will be made freestanding in the future standards. Regardless, containers likevector,list,deque, andmap will never be freestanding due to their dependencies on exceptions and heap.
GCC 13 provides more headers, such as<optional>,<span>,<array>, and<bitset>, for freestanding, although these headers may not be portable or provide the same capabilities as a hosted implementation. It is better to avoid using them in a freestanding environment, even if the toolchain provides them.
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
CWG 1938 | C++98 | an implementation did not need to document whether it is hosted | made the implementation kind implementation- defined (thus requires a documentation) |
LWG 3653 (P1642R11) | C++20 | <coroutine> is freestanding, but usesstd::hash which was not | made<functional> being partially freestanding |
C documentation forConformance |