泡蛇酒用什么药材最好| 怀孕分泌物是什么样的| 六月初一什么日子| 不正常的人有什么表现| 孙悟空姓什么| 梦到大牙掉了一颗是什么意思| 乳头湿疹用什么药| 给老师送什么礼物好| 胃疼买什么药| 关羽使用的武器是什么| bl什么意思| k值是什么意思| 牙齿突然酸痛什么原因| 男生生理期是什么表现| 低压高什么原因| 2004是什么年| 肠胃紊乱吃什么药| 勉铃是什么| 什么长而什么| 腰肌劳损有什么症状| 什么动物怕热| 上大便出血是什么原因| 去疤痕挂什么科| 京东京豆有什么用| 吃什么补脑子增强记忆力最快| 宫腔内异常回声是什么意思| 布病是什么症状| 什么是辐射| 什么情况下需要做造影| 运动裤配什么上衣好看| 2005年什么年| 矽肺是什么意思| 埃及法老是什么意思| 什么持不什么| 生物钟是什么| 砚是什么东西| 早上五点是什么时辰| 低频是什么意思| 紫砂壶适合泡什么茶| 猪油蒙了心是什么意思| 白血病有什么征兆| 发膜和护发素有什么区别| 正骨有什么好处和坏处| 买二手苹果手机要注意什么| 出类拔萃是什么意思| 感冒咳嗽一直不好是什么原因| csf是什么意思| 血糖用什么字母表示| 荔枝吃了有什么好处| 游园惊梦讲的是什么| 海丽汉森是什么档次| 相向而行是什么意思| 苍蝇是什么味道| 早餐吃什么比较好| 重症肌无力用什么药| 虫草花有什么功效和作用| 晚上喝柠檬水有什么好处| 郝字五行属什么| 艺术有什么用| 1月5号什么星座| 租赁费计入什么科目| 白斑不能吃什么| 安然无恙的恙是什么意思| 浙江大学校长什么级别| 五味子什么味道| 右手指发麻是什么原因| 一什么树林| 陈赫开的火锅店叫什么| 残局是什么意思| 尿毒症有什么症状| 氡气是什么| 苹果手机为什么那么贵| land rover是什么车| 雨字五行属什么| 等代表什么生肖| 荡秋千有什么好处| 隔离是什么意思| 肚脐眼是什么穴位| 乳房疼是什么原因| 云雾茶是什么茶| 开封有什么好玩的地方| 油菜花是什么颜色| 眉目传情什么意思| 鸭肉和什么一起炖好吃| 知音是什么意思| 备孕叶酸什么时候吃最好| 疑心病是什么意思| 淋巴细胞计数偏低是什么原因| 口腔溃疡看什么科室| 生酮是什么| 关爱是什么意思| 什么情况| 6月17号是什么星座| 姓陆的女孩取什么名字好| 哔哩哔哩是什么网站| 10月25日什么星座| ida是什么意思| 大方得体是什么意思| 气管疼什么原因引起的| 如来是什么意思| 11月17号是什么星座| 急性尿道炎吃什么药| 乐五行属性是什么| 为什么会尿路感染| 社科院是干什么的| 长期喝山楂水有什么好处和坏处| 量贩式ktv是什么意思| 木代表什么生肖| 水瓶男喜欢什么样的女生| 喉咙发痒咳嗽吃什么药| 牙龈萎缩吃什么维生素| co2是什么意思| 睡醒嘴苦是什么原因| 熟络是什么意思| 贞操是什么| 经血逆流的症状是什么| e是什么牌子| 心室早复极是什么意思| 阴道口瘙痒是什么原因| 588是什么意思| 流清鼻涕打喷嚏吃什么药| 猫的胡须有什么用处| pnh是什么病的简称| 干细胞是什么东西| 巨蟹座男和什么座最配对| 什么是脱敏| classic什么意思| 牙齿突然出血是什么原因| 新生儿眼屎多是什么原因| 66岁属什么生肖| 净土是什么意思| 刷牙时牙酸是什么原因| 来月经可以吃什么水果| 尿毒症什么原因引起的| 颌下淋巴结肿大挂什么科| 两个克念什么| 散瞳是什么| 紫丁香什么时候开花| 一声什么| 导师是什么意思| 羊水偏多对胎儿有什么影响| 梦见下大雪是什么意思| 湿疹涂什么| 什么的杏花| 肝病吃什么药好得快| 小肠炖什么好吃又营养| eb是什么| 紧锣密鼓是什么意思| 什么变什么化| 指背煞是什么意思| 女人大姨妈来了吃什么最好| 头皮长痘痘是什么原因| 高专是什么| 香蕉对身体有什么好处| 什么是类风湿性关节炎| bcm是什么意思| 咀嚼食用是什么意思| 慷慨解囊是什么意思| 放大镜是什么镜| 为什么会胃胀气| 绿豆配什么打豆浆最好| 女人肾虚是什么原因| 血糖高了会有什么危害| 烊化兑服是什么意思| 不明原因腿疼挂什么科| 印字五行属什么| 28岁今年属什么| 白细胞低是什么原因造成的| 荨麻疹要注意什么| 草金鱼吃什么| 什么食物降胆固醇最好| 女人为什么要穿高跟鞋| 刀口力念什么| 舌苔厚是什么原因引起的| 油炸食品用什么油最好| 巨蟹和什么星座最配| 为什么会有流星雨| 女人内心强大说明什么| 梦见别人杀人是什么预兆| 7月31日是什么星座| 胸部胀痛什么原因| 烂好人是什么意思| 闺蜜是什么样的关系| 子宫肌瘤吃什么药| 奋不顾身的顾是什么意思| 妇科检查白细胞酯酶阳性是什么意思| 回民不能吃什么| 脚气用什么药膏最好| 梦见着大火了是什么征兆| 什么食物补钙效果最好最快| 闪光点是什么意思| 小心眼是什么意思| 蚊子会传播什么疾病| 阴历六月是什么月| 生孩子大出血是什么原因造成的| 老鼠为什么怕猫| 蓝天白云是什么生肖| 胆囊炎的症状是什么| 双向情感障碍吃什么药| 八卦脸什么意思| 冬菜是什么菜| 喝莓茶对身体有什么好处| 白丝是什么| 软下疳是什么症状| 乌龟下蛋预示着什么| 心绞痛有什么症状| 天网是什么意思| 乳酸脱氢酶是什么| 戴的部首是什么| 胸外科主要看什么病| 小孩子黑眼圈重是什么原因| 三月18号是什么星座的| 压迫是什么意思| 鸭子吃什么食物| 护照需要什么材料| 吃得什么| 咖啡soe是什么意思| 尿崩症是什么症状| 猴跟什么生肖相冲| 香其酱是什么酱| 吃万艾可有什么副作用| 焖是什么意思| dsa检查是什么| 血糖高做什么运动好| 天天晚上睡觉做梦是什么原因| dha不能和什么一起吃| 710是什么意思| 多米诺骨牌是什么意思| 打hcg针有什么作用| 三十八岁属什么生肖| 腹泻能吃什么| 洋葱不能和什么一起吃| 尿发绿是什么原因| 身份证号最后一位代表什么| 心脏疼吃什么药效果好| 黄芪和什么泡水壮阳| 女宝胶囊的作用和功效是什么| 旻字五行属什么| 女人手心热吃什么药好| 啤酒是什么味道| 肝昏迷是什么症状| aone是什么牌子| 眼睛飞蚊症用什么眼药水| 艾滋病初期有什么症状| porsche是什么牌子的车| 萎缩性胃炎吃什么药最好| 脖子黑是什么病| 华在姓氏里读什么| 蜻蜓是什么目| 周边是什么意思| 金铃子是什么昆虫| 来月经前胸胀痛什么原因| 临床诊断是什么意思| 什么是阴唇| aigle是什么牌子| 胃气不足吃什么中成药| 日本为什么要偷袭珍珠港| 尿路结石有什么症状| 大小周休息是什么意思| ur品牌属于什么档次| 卫生纸属于什么垃圾| 没有斗代表什么| aoerbo是什么牌子的手表| 后背酸痛是什么原因| 百度Jump to content

探索不列颠哥伦比亚省

From Wikipedia, the free encyclopedia
Content deleted Content added
Line 115: Line 115:


=== Module purview and global module fragment ===
=== Module purview and global module fragment ===
In the above example, everything above the line <syntaxhighlight lang="C++" inline>export module myproject.MyClass;</syntaxhighlight> in the file {{mono|MyClass.cppm}} is referred to as what is "outside the module purview", meaning what is outside of the scope of the module.<ref name=cppreferencemodules /> Typically, if headers must be included, all <code>#include</code>s are placed outside the module purview between a line containing only the statement <syntaxhighlight lang="C++" inline>module;</syntaxhighlight> and the declaration of <code>export module</code>, like so:
In the above example, everything above the line <syntaxhighlight lang="C++" inline>export module myproject.MyClass;</syntaxhighlight> in the file {{mono|MyClass.cppm}} is referred to as what is "outside the module purview", meaning what is outside of the scope of the module.<ref name=cppreferencemodules /> Typically, all <code>#include</code>s are placed outside the module purview between the statement <syntaxhighlight lang="C++" inline>module;</syntaxhighlight> and the declaration of <code>export module</code>, like so:


<syntaxhighlight lang="cpp">
<syntaxhighlight lang="cpp">

Revision as of 23:20, 24 July 2025

  • Comment: Some large sections of unsourced text. Encoded  Talk ?? 22:01, 26 June 2025 (UTC)
  • Comment: (In response) I have reworded many parts of the article to be less essay-like and added official ISO C++ specification citations wherever applicable, I think it is adequately sourced now. 24.50.56.74 (talk) 23:17, 24 July 2025 (UTC)

百度   当前,我国正处于经济转轨、社会转型的关键时期,各种矛盾引发的涉农问题时有发生。


Modules in C++ are a modular programming feature added in C++20 as a modern alternative to precompiled headers.[1] A module in C++ comprises a single translation unit. Like header files and implementation files, a module can contain declarations and definitions, but differ from precompiled headers in that they do not require the preprocessor directive #include, but rather are accessed using the word import. A module must be declared using the word module to indicate that the translation unit is a module.[1] A module, once compiled, is stored as a .pcm (precompiled module) file which acts very similar to a .pch (precompiled header) file.[2]

Modules most commonly have the extension .cppm (primarily common within Clang and GCC toolchains), though some alternative extensions include .ixx and .mxx (more common in Microsoft/MSVC toolchains).[3]

Though the standard C language does not have modules, dialects of C allow for modules, such as Clang C.[4] However, the syntax and semantics of Clang C modules differ from C++ modules significantly.

Main uses

Modules provide the benefits of precompiled headers of faster compilation than #included traditional headers, as well as and faster processing during the linking phase.[5] This is because modules are not handled by the C preprocessor during the preprocessing step, but rather directly by the compiler during compilation. Modules also reduce boilerplate by allowing code to be implemented in a single file, rather than being separated across a header file and source implementation, although separation of "interface file" and "implementation file" is still possible with modules. Modules eliminate the necessity of #include guards or #pragma once, as modules do not directly modify the source code. Modules, unlike headers, do not have to be processed or recompiled multiple times.[5] However, similar to headers, any change in a module necessitates the recompilation of not only the module itself but also all its dependencies, and the dependencies of those dependencies, et cetera. Like headers, modules do not permit circular dependencies, and will not compile.[6]

A module is imported using the keyword import followed by a module name[a], while a module is declared with export module followed by the name. All symbols within a module meant to be exposed publicly are marked export, and importing the module exposes all exported symbols to the translation unit. If a module is never imported, it will never be linked.[7] Modules can export named symbols, but not macros which are consumed before compilation.

Unlike header inclusions, the order of import statements do not matter.[5] A module can allow for transitive imports by marking an import with export import, which re-exports the imported module to a translation unit that imports the first module.[1] Modules do not enforce any notion of namespaces, but it is not uncommon to see projects manually associate modules to namespaces (for example, a namespace like exampleproj::util::contents being tied to the module exampleproj.util.contents).[1] Because using statements will not be included into importing files (unless explicitly marked export), it is much less likely that using a using statement to bring symbols into the global namespace will cause name clashes within module translation units.

Currently, only GCC, Clang, and MSVC offer support for modules.[8]

Standard library modules

Since C++23, the C++ standard library has been exported as a module as well, though as of currently it must be imported in its entirety (using import std;).[9] The C++ standards offer two standard library modules:

Name Description
std Exports all declarations in namespace std and global storage allocation and deallocation functions that are provided by the importable C++ library headers including C library facilities (although declared in standard namespace).
std.compat Exports the same declarations as the named module std, and additionally exports functions in global namespace in C library facilities. It thus contains "compat" in the name, meaning compatibility with C.

The module names std and std.* are reserved by the C++ standard, and thus declaring a module whose name matches either pattern will issue a compiler warning.[10] However, most compilers provide a flag to bypass or suppress that warning (for example -Wno-reserved-module-identifier in Clang and GCC).[2]

Currently, only GCC, Clang, and MSVC support standard library modules.[11]

Example

A simple example of using modules is as follows:

MyClass.cppm

export module myproject.MyClass;

import std;

export namespace myproject {

class MyClass {
private:
    int x;
    std::string name;
public:
    MyClass(int x, const std::string& name):
        x{x}, name{name} {}

    [[nodiscard]]
    int getX() const noexcept {
        return x;
    }

    void setX(int newX) noexcept {
        x = newX;
    };

    [[nodiscard]]
    std::string getName() const noexcept {
        return name;
    }

    void setName(const std::string& newName) noexcept {
        name = newName;
    }
};

}

Main.cpp

import std;

import myproject.MyClass;

using myproject::MyClass;

int main(int argc, char* argv[]) {
    MyClass me(10, "MyName");
    me.setX(15);
    std::println("Hello, {0}! {0} contains value {1}.", me.getName(), me.getX());
}

Header units

Headers may also be imported using import, even if they are not declared as modules. Imported headers are called "header units", and are designed to allow existing codebases to migrate from headers to modules more gradually.[12][13] The syntax is similar to including a header, with the difference being that #include is replaced with import. As import statements are not preprocessor directives but rather full statements of the language read by the compiler, they must be terminated by a semicolon. Header units automatically export all symbols, and differ from proper modules in that they allow the emittance of macros, meaning all translation units that import the header unit will obtain its contained macros. This offers minimal breakage between migration to modules.[5] The semantics of searching for the file depending on whether quotation marks or angle brackets are used apply here as well. For instance, one may write import <string>; to import the <string> header, or import "MyHeader.h"; to import the file "MyHeader.h" as a header unit. Most build systems, such as CMake, do not support this feature yet.

Anatomy

Module partitions and hierarchy

Modules may have partitions, which separate the implementation of the module across several files. Module partitions are declared using the syntax A:B, meaning the module A has the partition B. Module partitions cannot individually be imported outside of the module that owns the partition itself, meaning that any translation unit that requires code located in a module partition must import the entire module that owns the partition.[1]

The module partition B is linked back to the owning module A with the statement import :B; in the file containing the declaration of module A or any other module partition of A (say A:C), which implicitly resolves :B to A:B, because the module is named A. These import statements may themselves be exported by the owning module, even if the partition itself cannot be imported directly, and thus importing code from a partition is done by just importing the entire module.

Other than partitions, modules do not have a hierarchical system or "submodules", but typically use a hierarchical naming convention, similar to Java's packages[b]. Only alphanumeric characters and the period and underscore may appear in the name of a module.[14] In C++, the name of a module is not tied to the name of its file or the module's location, unlike in Java[15], and the package it belongs to must match the path it is located in.[16] For example, the modules A and A.B in theory are disjoint modules and need not necessarily have any relation, however such a naming scheme is often employed to suggest that the module A.B is somehow related or otherwise associated with the module A.

The naming scheme of a C++ module is inherently hierarchical, and the C++ standard recommends re-exporting "sub-modules" belonging to the same public API (i.e. module alpha.beta.gamma should be re-exported by alpha.beta, etc.), even though dots in module names do not enforce any hierarchy. The C++ standard recommends lower-case ASCII module names (without hyphens or underscores), even though there is technically no restriction in such names.[17] Also, because modules cannot be re-aliased or renamed (short of re-exporting all symbols in another module), module names can be prefixed with organisation/project names for both clarity and to prevent naming clashes (i.e. google.abseil instead of abseil).[17] Also, unlike Java, whose packages may typically include a TLD to avoid namespace clashes, C++ modules need not have this convention.

Module purview and global module fragment

In the above example, everything above the line export module myproject.MyClass; in the file MyClass.cppm is referred to as what is "outside the module purview", meaning what is outside of the scope of the module.[1] Typically, all #includes are placed outside the module purview between the statement module; and the declaration of export module, like so:

module; // Optional; marks the beginning of the global module fragment (mandatory if an include directive is invoked above the export module declaration)

// Headers are included in outside the module purview, before the module is declared
#include <print>
#include "MyHeader.h"

export module myproject.MyModule; // Mandatory; marks the beginning of the module preamble

// Imports of named modules and header units come after the module declaration
// Import statements must be placed immediately after the module declaration and cannot come after any code or symbol declarations
// In non-module translation units, #include directives must precede import statements
import std;
import <string>;
import myproject.util.UtilitySymbols;
import "Foo.h";
import <thirdlib/features/Feature.h>;

// Code here...

module: private; // Optional; marks the beginning of the private module fragment

All code which does not belong to any module exists in the so-called "unnamed module" (also known as the global module fragment), and thus cannot be imported by any module.[1]

The file containing main() may declare a module, but typically it does not (as it is unusual to export main() as it is typically only used as an entry point to the program, and thus the file is usually a .cpp file and not a .cppm file). A program is ill-formed if it exports main() and doing so causes undefined behaviour[18], but will not necessarily be rejected by the compiler.

Private module fragment

A module may declare a "private module fragment" by writing module: private;, in which all declarations or definitions after the line are visible only from within the file and cannot be accessed by translation units that import that module.[1] Any module unit that contains a private module fragment must be the only module unit of its module.

See also

Notes

  1. ^ The import keyword in C++ differs in meaning than other languages. For instance, import in Java is actually analogous to using in C++ and not C++ import. In the former, an import simply aliases the type or de-qualifies a namespace, because Java loads .class files dynamically as necessary, thus making all types available simply by fully qualifying all namespaces (rather than having to explicitly declare accessible modules). However, in C++ modules are not automatically all linked, and thus they must be manually "imported" to be made accessible, as import indicates that the translation unit must access code in the imported module. Thus, it is probably more appropriate to compare import in C++ to mod in Rust, which "declares" or indicates to the compiler to find the module to link against.
  2. ^ It is more appropriate to compare packages in Java and modules in C++, rather than modules in Java and modules in C++. Modules in C++ and Java differ in meaning. In Java, a module (which is handled by the Java Platform Module System) is used to group several packages together, while in C++ a module is a translation unit, strictly speaking.

References

  1. ^ a b c d e f g h cppreference.com (2025). "Modules (since C++20)". cppreference.com. Retrieved 2025-08-05.
  2. ^ a b "Standard C++ Modules". clang.llvm.org.
  3. ^ "Overview of modules in C++". Microsoft. 24 April 2023.
  4. ^ "Modules". clang.llvm.org.
  5. ^ a b c d "Compare header units, modules, and precompiled headers". Microsoft. 12 February 2022.
  6. ^ ISO/IEC 14882:2020. Programming Languages – C++ (3rd ed.). International Organization for Standardization. §9.3, "Module interface units and import/export rules," and §16.3, "Module dependencies."
  7. ^ ISO/IEC 14882:2020. Programming Languages – C++ (3rd ed.). International Organization for Standardization. §9.3, "Module interface units and import/export rules," and §16.2, "Module import semantics."
  8. ^ "Compiler support for C++20". cppreference.com.
  9. ^ "Standard library - Importing modules". cppreference.com.
  10. ^ cppreference.com (2025). "C++ Standard Library". cppreference.com. Retrieved 2025-08-05.
  11. ^ "Compiler support for C++23". cppreference.com.
  12. ^ "Walkthrough: Build and import header units in Microsoft Visual C++". Microsoft. 12 April 2022.
  13. ^ "Standard C++ Modules - Header Units". clang.llvm.org.
  14. ^ ISO/IEC 14882:2020. Programming Languages – C++ (3rd ed.). International Organization for Standardization. §9.2, "Module interface units and import/export rules."
  15. ^ "Creating a Package". docs.oracle.com.
  16. ^ "Managing Source and Class Files". docs.oracle.com.
  17. ^ a b "Naming guidelines for modules". isocpp.org.
  18. ^ ISO/IEC 14882:2020. Programming Languages – C++ (3rd ed.). International Organization for Standardization. §3.6.1. "Program execution: the main() function."
汗手适合盘什么手串 什么叫假性发烧 纣王姓什么 胺试验阳性是什么意思 母亲节送妈妈什么礼物好
骨折后吃什么好 年薪20万算什么水平 癫疯病发作前兆有什么症状 灼热是什么意思 什么东西补肾
mcm中文叫什么牌子 没收个人全部财产是什么意思 身份证号码代表什么 支气管炎吃什么药效果最好 什么是多巴胺
小鹿乱撞是什么意思 吃什么能消除子宫肌瘤 检查耳朵挂什么科 乙型肝炎表面抗原阳性是什么意思 咖啡色五行属什么
感冒喉咙痛吃什么药hcv9jop4ns3r.cn 轻断食是什么意思hcv9jop5ns0r.cn 舌头有齿痕是什么原因hcv9jop4ns6r.cn 煮花生放什么调料好吃hcv8jop0ns3r.cn 黄芪主要治疗什么hcv8jop4ns3r.cn
小气道病变是什么意思hcv8jop4ns8r.cn 金字旁加者念什么hcv9jop2ns3r.cn 胡萝卜和什么不能一起吃hcv8jop8ns7r.cn 甲功五项查的是什么hcv9jop6ns8r.cn 1972年出生属什么生肖hcv9jop5ns5r.cn
桑螵蛸是什么hcv8jop5ns4r.cn 节育环是什么ff14chat.com 胃不好吃什么水果最好hcv9jop0ns7r.cn 一什么饭hcv9jop2ns8r.cn 爱情的本质是什么xjhesheng.com
肌酐低是什么问题hcv9jop0ns7r.cn 为什么今年有两个六月adwl56.com 打脸是什么意思hanqikai.com 什么是hcv9jop2ns1r.cn 骨折有什么忌口hcv9jop5ns6r.cn
百度