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]

More information Header, Part of the ...
HeaderPart of theFreestandingDescription
<cstddef>Language Support LibraryAllC standard definitions (e.g. NULL, size_t, etc.), as well as additional types (such as std::byte)
<cstdlib>Language Support LibraryPartialC standard library
<cfloat>, <climits>, <version> and <limits>Language Support LibraryAllDefinitions for the implementation defined properties
<cstdint>Language Support LibraryAllFixed width integer types
<new>Language Support LibraryAllMemory management (placement new, std::construct_at, std::destroy_at, std::launder etc.)
<typeinfo>Language Support LibraryAllType information and run-time type information
<exception>Language Support LibraryAllException handling support
<source_location>Language Support LibraryAllSource location information support
<initializer_list>Language Support LibraryAllInitializer list support
<compare>Language Support LibraryAllThree-way comparison (operator<=>) support
<coroutine>Language Support LibraryAllCoroutines support (since C++20)
<cstdarg>Language Support LibraryAllC-style variadic functions support
<debugging>Language Support LibraryAllDebugger support
<contracts>Language Support LibraryAllContracts support (since C++26)
<concepts>-AllConcepts support (since C++20)
<cerrno>Diagnostics LibraryPartialC-style error support (since C++26)
<system_error>Diagnostics LibraryPartialSystem errors support (since C++26)
<memory>Memory Management LibraryPartialMemory management support
<type_traits>Metaprogramming LibraryAllType-based programming support
<ratio>Metaprogramming LibraryAllCompile-time rational arithmetic support
<utility>General Utilities LibraryAllGeneral utilities like std::pair, std::index_sequence etc.
<tuple>General Utilities LibraryAllFixed size container that holds different types of values
<functional>General Utilities LibraryPartialFunction objects support
<charconv>General Utilities LibraryPartialPrimitive numeric and string conversions (since C++26)
<bit>General Utilities LibraryAllBit manipulation support (since C++20)
<stdbit.h>General Utilities LibraryAllC compatibility bit manipulation (since C++26)
<string>Strings LibraryPartialstd::string type and string classes (since C++26)
<string_view>Strings LibraryPartial (mostly freestanding)A view over string support (since C++26)
<cstring>Strings LibraryPartialNull-terminated strings support (since C++26)
<cwchar>Text Processing LibraryPartialC-compatibility header
<iterator>Iterators libraryPartialIterators support (since C++23)
<ranges>Ranges LibraryPartial (mostly freestanding with exceptions like std::ranges::views::istream_view)Ranges support (since C++23)
<cmath>Numerics LibraryPartialMathematical functions and utilities support (since C++26)
<random>Numerics LibraryPartialRandom number generation support
<atomic>Concurrency Support LibraryAllAtomic operations support
<execution>Execution Control LibraryUnspecifiedExecution control support (must provide at least std::is_execution_policy and std::is_execution_policy_v)
<span>Containers LibraryAllSupport for non-owning view over a container
<mdspan>Containers LibraryAllSupport for non-owning multi-dimensional view over a container
<optional>General Utilities LibraryPartial (mostly freestanding)A class for option types, representing either the presence or absence of an object of some type
<array>Containers LibraryPartial (mostly freestanding)A class abstracting a C-style array with fixed compile-time-specified size
<variant>General Utilities LibraryPartial (mostly freestanding)A type-safe union holding one value at a time
<expected>General Utilities LibraryPartial (mostly freestanding)A class for result types, typically holding either a returned value or an error code
<inplace_vector>Containers LibraryPartial (mostly freestanding)A fixed capacity, dynamically resizable array with contiguous in-place storage (since C++26)
<numeric>Algorithms LibraryPartial (mostly freestanding)Numeric operations over containers support
<algorithm>Algorithms LibraryPartial (mostly freestanding)Algorithms support
Close

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]

See also

References

Related Articles

Wikiwand AI