独立与宿主实现
来自cppreference.com
                    
                                        
                    < cpp
                    
                                                            
                    C++ 标准定义了两种实现:宿主 (hosted) 与独立 (freestanding) 实现。 C++ 标准对宿主实现所规定的标准库头文件集合比对独立实现所规定的大很多。独立实现中程序可能在没有操作系统的情况下运行。
实现的种类由实现定义。宏 __STDC_HOSTED__ 对宿主实现预定义为 1,对独立实现为 0。 (C++11 起)
| 针对多线程执行与数据竞争的规定
 | (C++11 起) | 
针对 main 函数的要求
| 独立 | 宿主 | 
|---|---|
| 独立实现中,是否要求程序定义 main 函数是由实现定义的。启动与终止过程是由实现定义的;启动过程中包含执行具有静态存储期的命名空间作用域对象的构造函数;终止过程中包含执行具有静态存储期的对象的析构函数。 | 宿主实现中,程序必须包含一个名为 main 的全局函数。程序执行时启动一个主执行线程,在其中调用 main函数,并且具有静态存储期的变量将在其中被初始化和销毁。 | 
针对标准库标头的规定
独立实现拥有由实现定义的标头集合。此集合至少包含下表中的标头:
| 类型 | <cstddef> | 
| 实现的属性 | <limits> <cfloat> <climits> (C++11 起) <version> (C++20 起) | 
| 整数类型 | <cstdint> (C++11 起) | 
| 启动与终止 | <cstdlib> (部分)[1] | 
| 动态内存管理 | <new> | 
| 类型鉴别 | <typeinfo> | 
| 源文件位置 | <source_location> (C++20 起) | 
| 异常处理 | <exception> | 
| 初始化器列表 | <initializer_list> (C++11 起) | 
| 比较 | <compare> (C++20 起) | 
| 协程支持 | <coroutine> (C++20 起) | 
| 其他运行时支持 | <cstdarg> | 
| 基础库概念 | <concepts> (C++20 起) | 
| 类型特征 | <type_traits> (C++11 起) | 
| 位操作 | <bit> (C++20 起) | 
| 原子类型 | <atomic> (C++11 起)[2] | 
| 实用工具组件 | <utility> (C++23 起) | 
| 多元组 | <tuple> (C++23 起) | 
| 内存 | <memory> (C++23 起) (部分) | 
| 函数对象 | <functional> (C++23 起) (部分) | 
| 编译时有理数算术 | <ratio> (C++23 起) | 
| 迭代器库 | <iterator> (C++23 起) (部分) | 
| 范围库 | <ranges> (C++23 起) (部分) | 
| 已弃用的头文件 | <ciso646> <cstdalign> <cstdbool> (C++11 起)(C++20 前) | 
- ↑ 头文件 <cstdlib>的受支持版本应当至少声明函数 std::abort 、 std::atexit 、 std::exit 、 std::at_quick_exit 及 std::quick_exit (C++11 起) 。
- ↑ 始终免锁的整数原子类型的支持,以及类型别名 std::atomic_signed_lock_free 与 std::atomic_unsigned_lock_free 的存在性在自立实现是实现定义的。 (C++20 起)
注解
编译器厂商可能不正确支持独立实现。原因要么是实现上存在问题,或者完全不支持独立实现 (LLVM libcxx和msvc stl).
很多特性将在C++23中标记为独立,但这些头文件都是部分的。至于array是否未来会独立仍然在讨论中。
引用
- C++20 标准(ISO/IEC 14882:2020):
- 4.1 Implementation compliance [intro.compliance](第 7 页)
 
- 6.9.2 Multi-threaded executions and data races [intro.multithread](第 77 页)
 
- 6.9.3.1 main function [basic.start.main](第 82 页)
 
- 16.5.1.3 Freestanding implementations [compliance](第 470 页)
 
- C++17 标准(ISO/IEC 14882:2017):
- 4.1 Implementation compliance [intro.compliance](第 5 页)
 
- 4.7 Multi-threaded executions and data races [intro.multithread](第 15 页)
 
- 6.6.1 main function [basic.start.main](第 66 页)
 
- 20.5.1.3 Freestanding implementations [compliance](第 458 页)
 
- C++14 标准(ISO/IEC 14882:2014):
- 1.4 Implementation compliance [intro.compliance](第 5 页)
 
- 1.10 Multi-threaded executions and data races [intro.multithread](第 11 页)
 
- 3.6.1 Main function [basic.start.main](第 62 页)
 
- 17.6.1.3 Freestanding implementations [compliance](第 441 页)
 
- C++11 标准(ISO/IEC 14882:2011):
- 1.4 Implementation compliance [intro.compliance](第 5 页)
 
- 1.10 Multi-threaded executions and data races [intro.multithread](第 11 页)
 
- 3.6.1 Main function [basic.start.main](第 58 页)
 
- 17.6.1.3 Freestanding implementations [compliance](第 408 页)
 
- C++03 标准(ISO/IEC 14882:2003):
- 1.4 Implementation compliance [intro.compliance](第 3 页)
 
- 3.6.1 Main function [basic.start.main](第 43 页)
 
- 17.4.1.3 Freestanding implementations [lib.compliance](第 326 页)
 
缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
| 缺陷报告 | 应用于 | 出版时的行为 | 正确行为 | 
|---|---|---|---|
| CWG 1938 | C++98 | 实现的文档不需要指明它是否有宿主 | 实现种类由实现定义(这样文档就需要指明) | 
| LWG 3653 | C++20 | <coroutine>是独立的, 但实现用的std::hash却不是。 | C++23将<functional>头文件部分独立 |