Freestanding (C++ implementation)
C/C++ term referring to an operating-system independent implementation
From Wikipedia, the free encyclopedia
Freestanding implementation is a C/C++ programming language term that refers to an implementation in which the execution of the program does not rely on an operating system.[1] Unlike a hosted implementation, a freestanding implementation does not provide the entire C standard library or C++ standard library, providing a non-operating system and non-dynamic memory-dependent subset.
Newer C/C++ standards and proposals have worked to extend the freestanding subset of the C and C++ standard libraries.[2]
Freestanding implementations are generally used for operating system development and embedded systems due to not relying on any operating system, where resource constraints are far more strict.
Differences between a freestanding and hosted implementation
In a hosted implementation, the program requires the entry point to be a global function called main(), while in a freestanding implementation it is implementation-defined. While _start() is typically the general default for the entry point function in many environments, this may be overridden.[3] Under a hosted implementation, a C++ program is required to support concurrently executing threads, while in a freestanding implementation, this is implementation defined.[1]
Under a freestanding implementation, the predefined macro __STDC_HOSTED__ will always expand to 0, while being expanded to 1 in hosted implementations.[1]
Header set
Although the header set of a freestanding implementation is implementation defined, the following headers are at least required for a freestanding implementation, partially and fully:[4]
| Header | Part of the | Freestanding | Description |
|---|---|---|---|
<cstddef> | Language Support Library | All | C standard definitions (e.g. NULL, size_t, etc.), as well as additional types (such as std::byte) |
<cstdlib> | Language Support Library | Partial | C standard library |
<cfloat>, <climits>, <version> and <limits> | Language Support Library | All | Definitions for the implementation defined properties |
<cstdint> | Language Support Library | All | Fixed width integer types |
<new> | Language Support Library | All | Memory management (placement new, std::construct_at, std::destroy_at, std::launder etc.) |
<typeinfo> | Language Support Library | All | Type information and run-time type information |
<exception> | Language Support Library | All | Exception handling support |
<source_location> | Language Support Library | All | Source location information support |
<initializer_list> | Language Support Library | All | Initializer list support |
<compare> | Language Support Library | All | Three-way comparison (operator<=>) support |
<coroutine> | Language Support Library | All | Coroutines support (since C++20) |
<cstdarg> | Language Support Library | All | C-style variadic functions support |
<debugging> | Language Support Library | All | Debugger support |
<contracts> | Language Support Library | All | Contracts support (since C++26) |
<concepts> | - | All | Concepts support (since C++20) |
<cerrno> | Diagnostics Library | Partial | C-style error support (since C++26) |
<system_error> | Diagnostics Library | Partial | System errors support (since C++26) |
<memory> | Memory Management Library | Partial | Memory management support |
<type_traits> | Metaprogramming Library | All | Type-based programming support |
<ratio> | Metaprogramming Library | All | Compile-time rational arithmetic support |
<utility> | General Utilities Library | All | General utilities like std::pair, std::index_sequence etc. |
<tuple> | General Utilities Library | All | Fixed size container that holds different types of values |
<functional> | General Utilities Library | Partial | Function objects support |
<charconv> | General Utilities Library | Partial | Primitive numeric and string conversions (since C++26) |
<bit> | General Utilities Library | All | Bit manipulation support (since C++20) |
<stdbit.h> | General Utilities Library | All | C compatibility bit manipulation (since C++26) |
<string> | Strings Library | Partial | std::string type and string classes (since C++26) |
<string_view> | Strings Library | Partial (mostly freestanding) | A view over string support (since C++26) |
<cstring> | Strings Library | Partial | Null-terminated strings support (since C++26) |
<cwchar> | Text Processing Library | Partial | C-compatibility header |
<iterator> | Iterators library | Partial | Iterators support (since C++23) |
<ranges> | Ranges Library | Partial (mostly freestanding with exceptions like std::ranges::views::istream_view) | Ranges support (since C++23) |
<cmath> | Numerics Library | Partial | Mathematical functions and utilities support (since C++26) |
<random> | Numerics Library | Partial | Random number generation support |
<atomic> | Concurrency Support Library | All | Atomic operations support |
<execution> | Execution Control Library | Unspecified | Execution control support (must provide at least std::is_execution_policy and std::is_execution_policy_v) |
<span> | Containers Library | All | Support for non-owning view over a container |
<mdspan> | Containers Library | All | Support for non-owning multi-dimensional view over a container |
<optional> | General Utilities Library | Partial (mostly freestanding) | A class for option types, representing either the presence or absence of an object of some type |
<array> | Containers Library | Partial (mostly freestanding) | A class abstracting a C-style array with fixed compile-time-specified size |
<variant> | General Utilities Library | Partial (mostly freestanding) | A type-safe union holding one value at a time |
<expected> | General Utilities Library | Partial (mostly freestanding) | A class for result types, typically holding either a returned value or an error code |
<inplace_vector> | Containers Library | Partial (mostly freestanding) | A fixed capacity, dynamically resizable array with contiguous in-place storage (since C++26) |
<numeric> | Algorithms Library | Partial (mostly freestanding) | Numeric operations over containers support |
<algorithm> | Algorithms Library | Partial (mostly freestanding) | Algorithms support |
Although not standard, some compilers like GCC and Clang partially support <chrono> and <bitset> headers in freestanding implementations, while Clang also provides header <generator>.[5][6]
Even though declarations of runtime allocator operator new is optional, transient constexpr allocations (by new/delete) and operator delete are still required for freestanding implementations.[7]
Newer proposals like P3295R3 suggest allowing containers like std::vector and allocators like std::allocator to be allowed at freestanding implementations only in transient constexpr allocations.[8]