Freestanding and hosted implementations - cppreference.com (original) (raw)

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 predefined macro __STDC_HOSTED__ is expanded to 1 for hosted implementations and ​0​ for freestanding implementations.(since C++11)

Contents 1 Requirements on multi-threaded executions and data races 2 Requirements on the main function 3 Requirements on standard library headers 3.1 Headers required for a freestanding implementation 4 Notes 5 References 6 Defect reports 7 See also Requirements on multi-threaded executions and data races freestanding hosted Under a freestanding implementation, it is implementation-defined whether a program can have more than one thread of execution. Under a hosted implementation, a C++ program can have more than one thread running concurrently. (since C++11)

[edit] Requirements on the main function

freestanding 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.

[edit] Requirements on standard library headers

A freestanding 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:

If an entity (function or function template) is commented // freestanding-deleted, it is guaranteed to be either provided or deleted. (since C++26)

[edit] Headers required for a freestanding implementation

  1. Support for always lock-free integral atomic types and presence of type aliases std::atomic_signed_lock_free and std::atomic_unsigned_lock_free are implementation-defined in a freestanding implementation.(since C++20)

[edit] Notes

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 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 , , , and , 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.

Feature-test macro Value Std Feature
__cpp_lib_freestanding_feature_test_macros 202306L (C++26) freestanding feature test macros
__cpp_lib_freestanding_algorithm 202311L (C++26) freestanding
202502L (C++26) more freestanding facilities in
__cpp_lib_freestanding_array 202311L (C++26) freestanding
__cpp_lib_freestanding_char_traits 202306L (C++26) freestanding std::char_traits
__cpp_lib_freestanding_charconv 202306L (C++26) freestanding
__cpp_lib_freestanding_cstdlib 202306L (C++26) freestanding
__cpp_lib_freestanding_cstring 202311L (C++26) freestanding
__cpp_lib_freestanding_cwchar 202306L (C++26) freestanding
__cpp_lib_freestanding_errc 202306L (C++26) freestanding std::errc
__cpp_lib_freestanding_execution 202502L (C++26) freestanding
__cpp_lib_freestanding_expected 202311L (C++26) freestanding
__cpp_lib_freestanding_functional 202306L (C++26) freestanding
__cpp_lib_freestanding_iterator 202306L (C++26) freestanding
__cpp_lib_freestanding_mdspan 202311L (C++26) freestanding
__cpp_lib_freestanding_memory 202306L (C++26) freestanding
202502L (C++26) more freestanding facilities in
__cpp_lib_freestanding_numeric 202311L (C++26) freestanding
202502L (C++26) more freestanding facilities in
__cpp_lib_freestanding_optional 202311L (C++26) freestanding
__cpp_lib_freestanding_random 202502L (C++26) freestanding
__cpp_lib_freestanding_ranges 202306L (C++26) freestanding
__cpp_lib_freestanding_ratio 202306L (C++26) freestanding
__cpp_lib_freestanding_string_view 202311L (C++26) freestanding <string_view>
__cpp_lib_freestanding_tuple 202306L (C++26) freestanding
__cpp_lib_freestanding_utility 202306L (C++26) freestanding
__cpp_lib_freestanding_variant 202311L (C++26) freestanding

[edit] References

[edit] Defect reports

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 needto document whether it is hosted made the implementation kind implementation-defined (thus requires a documentation)
LWG 3653(P1642R11) C++20 is freestanding, butuses std::hash which was not made being partially freestanding

[edit] See also