C ++ 20 #import“"语句:是否可以使用多个预处理的头文件 [英] C++20 #import ""-statement: Will it be possible to use multiple preprocessed header files

查看:121
本文介绍了C ++ 20 #import“"语句:是否可以使用多个预处理的头文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++ 20中,可以用

代替包含标头

In C++20 it is possible to instead of include headers with

#include "header.h"

使用

#import "header.h" // Edit: this is not standard see comments

(这是针对无法完全转换为模块并且无法使用不带#的常规import关键字的旧代码)

(this is for old code that cannot fully be converted to modules and use the normal import keyword without #)

我的问题是关于预处理.长期以来,每个翻译单元只能预编译和使用一个标头.(Clang似乎在级联包含文件时有一些特殊情况,我在这里不考虑)

My question is about preprocessing. For a long time it as only been possible to precompile and use one header per translation unit. (Clang seems to have some special case with cascading include files, that I do not consider here)

既然头文件可以在某种程度上隔离了,那么是否可以通过 #import 关键字对每个翻译单元进行预编译和使用多个头文件?

Will it be possible to precompile and use multiple header files per translation unit with the #import keyword, now that the header files can be somewhat isolated?

所以我对此的误解似乎来自以下事实:当在例如g ++中启用模块时,使用 #include"..." 会变成 import"; 自动(我认为).

So my misunderstanding of this seemed to be from the fact that, when enabling modules in for example g++, using #include "..." turns into import "" automatically (I think).

我发现了这部视频 Nathan Sidwell转换为C ++ 20模块" 内森(Nathan)解释了如何预编译标头单元",我猜想是g ++中的modules分支,但在clang ++-10中似乎不起作用.我不知道这是否也可以在其他编译器上使用.

I found this video Nathan Sidwell "Converting to C++20 Modules" where Nathan explains how to precompile "header units", in what I guess is the modules branch in g++, but it does not seem to work in clang++-10. I don't know if this is something that will work on other compilers as well.

推荐答案

是的,可以导入 任意数量的标头单元到翻译单元中.由于模块规则可防止模块之间进行任何交互(特别是它们不能共享宏),因此可以独立地对其进行预编译".也就是说,某些实现可能会从生成的已编译模块接口文件中将某些组件复制到正在编译的翻译单元中,以更好地模拟文本包含的行为(尤其是结合标头中的可疑声明,例如带有内部链接).

Yes, it is possible to import any number of header units into a translation unit. Because the rules for modules prevent any interaction between them (in particular, they cannot share macros), they can all be "precompiled" independently. That said, some implementations may copy certain components from the resulting compiled module interface files into the translation unit being compiled to better emulate the behavior of textual inclusion (especially in combination with questionable declarations in the header like those with internal linkage).

单独地,将 #include 应用于实现已知(可能来自某些选项或配置文件)可用作头单元的头文件,可以重新解释 import 可以为未修改的文件(尤其是您无法控制的另一个库中的头文件)提供这种行为.

Separately, #include applied to a header file which the implementation knows (perhaps from some option or configuration file) to be usable as a header unit may be reinterpreted as import to provide this behavior even for unmodified files (in particular, for header files from another library that you don’t control).

这篇关于C ++ 20 #import“"语句:是否可以使用多个预处理的头文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆