There are two kinds of implementations defined by the C++ standard: hosted and freestanding implementations. For hosted implementations, the set of standard library headers required by the C++ standard is much larger than for freestanding ones. In a freestanding implementation, execution may happen without an operating system.
The kind of the implementation is implementation-defined. The macro __STDC_HOSTED__
is predefined to 1
for hosted implementations and 0
for freestanding implementations. (since C++11).
Requirements on multi-threaded executions and data races
| (since C++11) |
main
functionfreestanding | hosted |
---|---|
In a freestanding implementation, it is implementation-defined whether a program is required to define a main function. Start-up and termination is implementation-defined; start-up contains the execution of constructors for objects of namespace scope with static storage duration; termination contains the execution of destructors for objects with static storage duration. | In a hosted implementation, a program must contain a global function called main . Executing a program starts a main thread of execution in which the main function is invoked, and in which variables of static storage duration might be initialized and destroyed. |
A freestanding implementation has an implementation-defined set of headers. This set includes at least the headers in the following table:
Types | <cstddef> |
Implementation properties | <limits> <cfloat> <climits> (since C++11)<version> (since C++20) |
Integer types | <cstdint> (since C++11) |
Start and termination | <cstdlib> (partial)[1] |
Dynamic memory management | <new> |
Type identification | <typeinfo> |
Source location | <source_location> (since C++20) |
Exception handling | <exception> |
Initializer lists | <initializer_list> (since C++11) |
Comparisons | <compare> (since C++20) |
Coroutines support | <coroutine> (since C++20) |
Other runtime support | <cstdarg> |
Fundamental library concepts | <concepts> (since C++20) |
Type traits | <type_traits> (since C++11) |
Bit manipulation | <bit> (since C++20) |
Atomics | <atomic> (since C++11)[2] |
Utility components | <utility> (since C++23) |
Tuples | <tuple> (since C++23) |
Memory | <memory> (since C++23) (partial) |
Function objects | <functional> (since C++23) (partial) |
Compile-time rational arithmetic | <ratio> (since C++23) |
Iterators library | <iterator> (since C++23) (partial) |
Ranges library | <ranges> (since C++23) (partial) |
Deprecated headers | <ciso646> (until C++20)<cstdalign> (since C++11)(until C++20)<cstdbool> (since C++11)(until C++20) |
<cstdlib>
shall declare at least the functions std::abort
, std::atexit
, std::exit
, std::at_quick_exit
and std::quick_exit
(since C++11). std::atomic_signed_lock_free
and std::atomic_unsigned_lock_free
are implementation-defined in a freestanding implementation. (since C++20) It's important to note that 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 will be made freestanding with partial headers. However, it's still up for discussion in WG21 whether headers like <array> will be made freestanding in the future standard (C++26, maybe). Regardless, containers like vector, list, deque, and map 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. However, it's important to note that these headers may not be portable or provide the same experience as a hosted implementation. It's 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 | C++20 | <coroutine> is freestanding, but uses std::hash which is not | Fixed in C++23 with <functional> being partial freestanding |
C documentation for Conformance |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/freestanding