C++ 模块 - 为什么它们从 C++0x 中删除?他们以后会回来吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3596147/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 13:14:07  来源:igfitidea点击:

C++ Modules - why were they removed from C++0x? Will they be back later on?

c++c++11modulestandardslanguage-extension

提问by Tomaka17

I just discovered this old C++0x draftabout modules in C++0x.

我刚刚发现了这个关于 C++0x 模块的旧 C++0x 草案

The idea was to get out of the current .h/.cpp system by writing only .cpp files which would then generate module files during compilation, which would then in turn be used by the other .cpp files.

这个想法是通过只编写 .cpp 文件来摆脱当前的 .h/.cpp 系统,然后在编译期间生成模块文件,然后由其他 .cpp 文件使用。

This looks like a really great feature.

这看起来是一个非常棒的功能。

But my question is: why did they remove it from C++0x? Was it because of too many technical difficulties? Lack of time? And do you think they will consider working on it for an ulterior version of C++?

但我的问题是:他们为什么要从 C++0x 中删除它?是不是因为技术难度太大?时间不够?你认为他们会考虑为 C++ 的另一个版本开发它吗?

采纳答案by James McNellis

From the State of C++ Evolution (Post San Francisco 2008), the Modules proposal was categorized as "Heading for a separate TR:"

C++ 演进状态(旧金山后 2008 年),模块提案被归类为“标题为单独的 TR:”

These topics are deemed too important to wait for another standard after C++0x before being published, but too experimental to be finalised in time for the next Standard. Therefore, these features will be delivered by a technical report at the earliest opportunity.

这些主题被认为太重要了,不能在发布之前等待 C++0x 之后的另一个标准,但又太具有实验性,无法及时为下一个标准定稿。因此,这些功能将尽早通过技术报告提供。

The modules proposal just wasn't ready and waiting for it would have delayed finishing the C++0x standard. It wasn't really removed, it was just never incorporated into the working paper.

模块提案还没有准备好,等待它会延迟完成 C++0x 标准。它并没有真正被删除,只是从未被纳入工作文件。

回答by lanoxx

C++ Modules draft (Technical Specification after C++17)

C++ 模块草案(C++17 之后的技术规范)

A draft and several updated revisions for the C/C++ module specification have been published by WG21on open-std.org. I will link only to the latest documents here:

WG21已经在open-std.org上发布了 C/C++ 模块规范的草案和几个更新的修订版。我只会在这里链接到最新的文件:

  • Working Draft, Extensions to C++ for Modules N4610(October 2016).
  • Fourth revision published as P0142R0(March 2016).
  • Wording for Modules published as P0143R2(March 2016).
  • The clang team has published a second revision of their changes: P0273R1(October 2016).
  • 工作草案,模块N4610 的C++ 扩展(2016 年 10 月)。
  • 第四次修订发布为P0142R0(2016 年 3 月)。
  • 发布为P0143R2(2016 年 3 月)的模块的措辞。
  • clang 团队发布了他们更改的第二个修订版:P0273R1(2016 年 10 月)。

The following blog posts contain a summary of the standards meetings and in particular a summary of the current status of the modules draft:

以下博客文章包含标准会议的摘要,特别是模块草案当前状态的摘要:

Update:As explained in the Kona trip report that I linked to above, there are currently two competing proposals, one from Microsoft and one from Clang. The proposed solution from Microsoft does not allow to export Macros, while the solution from the Clang team would support exporting Macros. So far only Microsoft has formally submitted a draft for a module specification.

更新:正如我在上面链接的 Kona 旅行报告中所解释的,目前有两个相互竞争的提案,一个来自 Microsoft,一个来自 Clang。Microsoft 提出的解决方案不允许导出宏,而 Clang 团队的解决方案将支持导出宏。到目前为止,只有微软正式提交了模块规范的草案。

Module specification as proposed by Microsoft

Microsoft 提出的模块规范

Here is a quick overview of the most important concepts that this proposal contains. As its a draft this might possibly still change. The new modules standard will among other things consist of the following:

以下是该提案包含的最重要概念的快速概览。作为草案,这可能仍然会改变。新模块标准将包括以下内容:

A modulekeyword to declare a module, multiple files can declare this to build one module (but for each module only one compilation-unitcan contain an export {}section):

一个module用于声明模块的关键字,多个文件可以声明它来构建一个模块(但对于每个模块,只有一个编译单元可以包含一个export {}节):

module M;

An importkeyword to import modules, instead of importit might also be decided to use using moduleinstead, so a new import keyword could be avoided.

import导入模块的关键字,而不是import它也可能决定使用using module,因此可以避免使用新的导入关键字。

import std.io;
import module.submodule;

An exportsyntax, which defines the public declarationsthat are part of this module, non-interface declarationsthat should not be exported as part of the module will be defined outside the export block. Declarationscan be any kind of declaration in C/C++, that is, not only functions but also variables, structs, templates, namespaces and classes:

export定义作为该模块一部分的公共声明的语法,不应作为模块的一部分导出的非接口声明将在导出块之外定义。声明可以是 C/C++ 中的任何类型的声明,即不仅可以是函数,还可以是变量、结构体、模板、命名空间和类:

export {
    int f(int);
    double g(double, int);

    int foo;

    namespace Calc {
         int add(int a, int b);
    }        
}

void not_exported_function(char* foo);

An important change of modules will be that macros and preprocessor definitions will be local to modules and will not be exported. Thus macros do not have any impact on imported modules:

模块的一个重要变化是宏和预处理器定义将是模块本地的,不会被导出。因此宏对导入的模块没有任何影响:

#define FILE "my/file"
import std.io;   //will not be impacted by the above definition

Its important note that the both the current preprocessor system and modules will be able to co-exist and headers can still be used for example to include macros.

重要的是,当前的预处理器系统和模块将能够共存,并且标头仍然可以用于例如包含宏。

For more detailed information I suggest to read the draft.

有关更多详细信息,我建议阅读草稿。

Clang Modules

Clang 模块

Clang has been working on a modules implementation which can be found at the clang modules page. However clang does currently not implement a concrete syntax for modules, that is, none of the above mentioned syntax has been implemented by Clang. To explain this the page contains the following statement:

Clang 一直致力于模块实现,可以在clang 模块页面上找到。然而,clang 目前没有为模块实现具体的语法,也就是说,上面提到的语法都没有被 Clang 实现。为了解释这一点,该页面包含以下声明:

At present, there is no C or C++ syntax for import declarations. Clang will track the modules proposal in the C++ committee. See the section Includes as imports to see how modules get imported today.

目前,没有用于导入声明的 C 或 C++ 语法。Clang 将跟踪 C++ 委员会中的模块提案。请参阅作为导入包含部分以了解今天如何导入模块。

The main part that is currently implemented by Clang is the "Module Map Language" which allows write module maps for existing code that still uses header files.

当前由 Clang 实现的主要部分是“模块映射语言”,它允许为仍然使用头文件的现有代码编写模块映射。

Macro Exports from Modules

从模块导出宏

As mentioned above it is still unclear if macro exports will be part of the final Modules TS. In P0273R1the following syntax was proposed for the export of macros:

如上所述,目前尚不清楚宏导出是否会成为最终模块 TS 的一部分。在P0273R1 中,为导出宏提出了以下语法:

#export define MAX(A,B) ((A) > (B)) ? (A) : (B);

回答by zah

Clang is the first compiler to start working on modules even before the standardization is complete. There is not much of a documentation yet, but example code could be found here:
http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Modules/

Clang 是第一个在标准化完成之前就开始处理模块的编译器。目前还没有太多文档,但可以在此处找到示例代码:http:
//llvm.org/viewvc/llvm-project/cfe/trunk/test/Modules/

Some comments from Douglas Gregor (the developer implementing them):
http://clang-developers.42468.n3.nabble.com/C-modules-td3619936.html

Douglas Gregor(实现它们的开发人员)的一些评论:http:
//clang-developers.42468.n3.nabble.com/C-modules-td3619936.html

In theory, you can define a bunch of helper macros like begin_module, end_module, import_module to shield yourself from any likely changes to the syntax that will come in the future.

理论上,您可以定义一堆辅助宏,如 begin_module、end_module、import_module 以保护自己免受将来可能发生的任何语法更改。

EDIT 1:
Douglas Gregor has released a presentation about his implementation:
http://llvm.org/devmtg/2012-11/Gregor-Modules.pdf?=submit

编辑 1:
Douglas Gregor 发布了关于他的实现的演示文稿:http:
//llvm.org/devmtg/2012-11/Gregor-Modules.pdf?=submit

EDIT 2:
The module support in clang have been documented here:
http://clang.llvm.org/docs/Modules.html

编辑 2:
clang 中的模块支持已在此处记录:http:
//clang.llvm.org/docs/Modules.html

EDIT 3:
Modules are now supported in Microsoft's C++ compiler as well: http://blogs.msdn.com/b/vcblog/archive/2015/12/03/c-modules-in-vs-2015-update-1.aspx

编辑 3:
Microsoft 的 C++ 编译器现在也支持模块:http: //blogs.msdn.com/b/vcblog/archive/2015/12/03/c-modules-in-vs-2015-update-1。 aspx

回答by Artyom

  1. Because it is very big conceptual change.
  2. There is no real need of it as separation of the sources to h/cpp does the job
  3. Because C++ does not define how actual "modules" libraries are build. It leaves it to compiler developer and to linker.
  4. "Modules" are sometimes quite platform dependent, for example DLLs quite different from shared objects. So it is not so trivial to merge between these concepts.
  1. 因为这是非常大的概念变化。
  2. 没有真正需要它,因为将源分离到 h/cpp 就可以了
  3. 因为 C++ 没有定义实际的“模块”库是如何构建的。它留给编译器开发人员和链接器。
  4. “模块”有时非常依赖于平台,例如 DLL 与共享对象完全不同。因此,在这些概念之间进行合并并非易事。