为什么在模块中导出类型别名(例如 std::vector )允许在某些内部分区中同时使用 std::vector 和 std::string ?

2024-04-03

我目前正在使用 Visual Studio 2022 Update 17.1.6,我发现导出类型别名有一些有趣的东西。由于我不明白的原因,当我导出某些数据类型的类型别名时,例如std::vector<std::string>在模块接口文件中,我可以同时使用std::vector<> and std::string在导入它的文件中。例如:

modInterface.ixx

export module words;
import <iostream>
import <vector>;
import <string>;
...
export using Words = std::vector<std::string>;
...

在内部分区中:

modInternalPartition.cpp

module words:wordsIP;
import words;

//This compiles as expected
Words wordStorage;

//Why does my compiler sees below as correct, and compiles it without error?
std::vector<int> numStorage = { 1, 2, 3, 4 };

//Why does my compiler also sees below as correct, and compiles it without error?
std::string text = "This dish is tasty";

//This would produce an error, which is expected since I did not export import <iostream> in modInterface.ixx
std::cout << text;
...

我的第一个想法是,自从Words是一个类型别名,导出它就意味着导出std::vector<> and std::string, 但是由于std::vector<>是一个模板,为什么不是只有它的实例化(std::vector<std::string>) 被导出?


当一part一个模块导入另一个模块时,所有不受内部链接影响的声明都可用,就好像它们是一样exported。这甚至适用于import声明(以及模块的非分区实现单元的隐式导入),因此import <vector>; etc.可以在words:wordsIP分割。这export与此无关。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

为什么在模块中导出类型别名(例如 std::vector )允许在某些内部分区中同时使用 std::vector 和 std::string ? 的相关文章

随机推荐