Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      Freestanding and hosted implementations

      From cppreference.com
      <cpp
       
       

      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)

      Contents

      Requirements onmulti-threaded executions and data races

      freestandinghosted
      Under afreestanding implementation, it is implementation-defined whether a program can have more than onethread of execution. Under ahosted implementation, a C++ program can have more than onethread running concurrently.
      (since C++11)

      [edit]Requirements on themain function

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

      [edit]Requirements onstandard library headers

      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:

      • If an entity is commented// freestanding, it is guaranteed to be provided.
      • If an entity (function or function template) is commented// freestanding-deleted, it is guaranteed to be either provided or deleted.
      (since C++26)
      • If an entity is declared in a header whose synopsis begins withall freestanding or// mostly freestanding, it is guaranteed to be provided if the entity itself is not commented.

      [edit]Headers required for a freestanding implementation

      LibraryComponentHeaders Freestanding 
      Language supportCommon 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
      DiagnosticsError numbers<cerrno>(since C++26)Partial
      System error support<system_error>(since C++26)Partial
      Memory management Memory<memory>(since C++23)Partial
      MetaprogrammingType traits<type_traits>(since C++11)All
      Compile-time rational arithmetic <ratio>(since C++23)All
      General utilitiesUtility 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
      StringsString classes<string>(since C++26)Partial
      Null-terminated
      sequence utilities
      <cstring>(since C++26)Partial
      Text processingNull-terminated
      sequence utilities
      <cwchar>(since C++26)Partial
      Iterators<iterator>(since C++23)Partial
      Ranges<ranges>(since C++23)Partial
      NumericsMathematical functions
      for floating-point types
      <cmath>(since C++26)Partial
      Random number generation<random>(since C++26)Partial
      Concurrency supportAtomics<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
      1. Support for always lock-free integral atomic types and presence of type aliasesstd::atomic_signed_lock_free andstd::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 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.

      Feature-test macroValueStdFeature
      __cpp_lib_freestanding_feature_test_macros202306L(C++26)freestanding feature test macros
      __cpp_lib_freestanding_algorithm202311L(C++26)freestanding<algorithm>
      202502L(C++26)more freestanding facilities in<algorithm>
      __cpp_lib_freestanding_array202311L(C++26)freestanding<array>
      __cpp_lib_freestanding_char_traits202306L(C++26)freestandingstd::char_traits
      __cpp_lib_freestanding_charconv202306L(C++26)freestanding<charconv>
      __cpp_lib_freestanding_cstdlib202306L(C++26)freestanding<cstdlib>
      __cpp_lib_freestanding_cstring202311L(C++26)freestanding<cstring>
      __cpp_lib_freestanding_cwchar202306L(C++26)freestanding<cwchar>
      __cpp_lib_freestanding_errc202306L(C++26)freestandingstd::errc
      __cpp_lib_freestanding_execution202502L(C++26)freestanding<execution>
      __cpp_lib_freestanding_expected202311L(C++26)freestanding<expected>
      __cpp_lib_freestanding_functional202306L(C++26)freestanding<functional>
      __cpp_lib_freestanding_iterator202306L(C++26)freestanding<iterator>
      __cpp_lib_freestanding_mdspan202311L(C++26)freestanding<mdspan>
      __cpp_lib_freestanding_memory202306L(C++26)freestanding<memory>
      202502L(C++26)more freestanding facilities in<memory>
      __cpp_lib_freestanding_numeric202311L(C++26)freestanding<numeric>
      202502L(C++26)more freestanding facilities in<numeric>
      __cpp_lib_freestanding_optional202311L(C++26)freestanding<optional>
      __cpp_lib_freestanding_random202502L(C++26)freestanding<random>
      __cpp_lib_freestanding_ranges202306L(C++26)freestanding<ranges>
      __cpp_lib_freestanding_ratio202306L(C++26)freestanding<ratio>
      __cpp_lib_freestanding_string_view202311L(C++26)freestanding<string_view>
      __cpp_lib_freestanding_tuple202306L(C++26)freestanding<tuple>
      __cpp_lib_freestanding_utility202306L(C++26)freestanding<utility>
      __cpp_lib_freestanding_variant202311L(C++26)freestanding<variant>

      [edit]References

      • C++23 standard (ISO/IEC 14882:2024):
      • 4.1 Implementation compliance [intro.compliance] (p: 10)
      • 6.9.2 Multi-threaded executions and data races [intro.multithread] (p: 84)
      • 6.9.3.1 main function [basic.start.main] (p: 89)
      • 16.4.2.5 Freestanding implementations [compliance] (p: 483)
      • C++20 standard (ISO/IEC 14882:2020):
      • 4.1 Implementation compliance [intro.compliance] (p: 7)
      • 6.9.2 Multi-threaded executions and data races [intro.multithread] (p: 77)
      • 6.9.3.1 main function [basic.start.main] (p: 82)
      • 16.5.1.3 Freestanding implementations [compliance] (p: 470)
      • C++17 standard (ISO/IEC 14882:2017):
      • 4.1 Implementation compliance [intro.compliance] (p: 5)
      • 4.7 Multi-threaded executions and data races [intro.multithread] (p: 15)
      • 6.6.1 main function [basic.start.main] (p: 66)
      • 20.5.1.3 Freestanding implementations [compliance] (p: 458)
      • C++14 standard (ISO/IEC 14882:2014):
      • 1.4 Implementation compliance [intro.compliance] (p: 5)
      • 1.10 Multi-threaded executions and data races [intro.multithread] (p: 11)
      • 3.6.1 Main function [basic.start.main] (p: 62)
      • 17.6.1.3 Freestanding implementations [compliance] (p: 441)
      • C++11 standard (ISO/IEC 14882:2011):
      • 1.4 Implementation compliance [intro.compliance] (p: 5)
      • 1.10 Multi-threaded executions and data races [intro.multithread] (p: 11)
      • 3.6.1 Main function [basic.start.main] (p: 58)
      • 17.6.1.3 Freestanding implementations [compliance] (p: 408)
      • C++03 standard (ISO/IEC 14882:2003):
      • 1.4 Implementation compliance [intro.compliance] (p: 3)
      • 3.6.1 Main function [basic.start.main] (p: 43)
      • 17.4.1.3 Freestanding implementations [lib.compliance] (p: 326)

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      CWG 1938C++98an 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

      [edit]See also

      C documentation forConformance
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/freestanding&oldid=182197"

      [8]ページ先頭

      ©2009-2025 Movatter.jp