c++ - 在 C++20 中混合模块和头文件是否可能/可接受?
问题描述
我实际上是在尝试通过编写自己的小模块来理解 C++20 模块系统。假设我想提供一个函数来删除字符串开头和结尾的所有空格(一个trim
函数)。以下代码可以正常工作。
module;
export module String;
import std.core;
export std::string delete_all_spaces(std::string const & string)
{
std::string copy { string };
auto first_non_space { std::find_if_not(std::begin(copy), std::end(copy), isspace) };
copy.erase(std::begin(copy), first_non_space);
std::reverse(std::begin(copy), std::end(copy));
first_non_space = std::find_if_not(std::begin(copy), std::end(copy), isspace);
copy.erase(std::begin(copy), first_non_space);
std::reverse(std::begin(copy), std::end(copy));
return copy;
}
import std.core;
import String;
int main()
{
std::cout << delete_all_spaces(" Hello World! \n");
return 0;
}
但是如果我只想使用特定的标头而不是std.core
在我的模块中呢?如果我这样做,用import std.core
以下代码替换,我会在 Visual Studio 2019 上收到错误。
module;
#include <algorithm>
#include <cctype>
#include <string>
export module String;
// No more import of std.core
export std::string delete_all_spaces(std::string const & string)
{
// ...
}
Error LNK1179 file not valid or damaged: '??$_Deallocate@$07$0A@@std@@YAXPAXI@Z' COMDAT duplicated
但是,如果在main.cpp
I 中也替换了import std.core
with #include <iostream>
,则代码会再次编译。这就像使用两个系统证明链接器来完成它的工作。
问题是:我做错了吗?import
同时使用新方法和旧#include
方法是一种不好的做法吗?我在 Internet 上的多个帖子中看到,您可以在模块中包含一些旧标题,从而在不破坏现有代码的情况下更新您的代码。但是,如果此标头包含 STL 的某些部分,比如#include <string>
我的模块使用了import std.core
怎么办?
我只使用 Visual Studio 2019 进行测试,因为到目前为止,import std.core
它不适用于 GCC。那么,它可能来自 VS 中的错误吗?还是所有编译器的问题都一样?
解决方案
是的,模块可以与头文件一起使用。我们可以在同一个文件中导入和包含头文件,这是一个例子:
import <iostream>
#include <vector>
int main()
{
std::vector<int> v{1,6,8,7};
for (auto i:v)
std::cout<<i;
return 0;
}
创建模块时,您可以自由地在模块的接口文件中导出实体并将实现移动到其他文件。总而言之,逻辑与管理 .h 和 .cpp 文件的逻辑相同
推荐阅读
- amazon-web-services - 使用 AWS 服务和 Mapbox api 在 Mapbox 上上传一个大数据集 .geojson 文件
- vba - Excel VBA:通过单击按钮事件更新图表 X 轴最小值、最大值和单位值
- php - Howto ubuntu 18.04 安装/激活 PHP 扩展 ext-mcrypt
- django - Django 应用程序在 Heroku 上共享数据库:一次性设置“共享”数据库
- python - 从Python中的字符串中删除特定数量的重复字母
- java - EasyMock 和参数化测试(JUnit 参数化)
- tensorflow - 将自定义输入管道连接到 tf 模型
- javascript - HTML/JS 增量游戏中的错误
- php - 带有 CakePHP 3 的 Gitlab CI
- artifactory - 在 Artifactory 的 Web GUI 中部署文件会生成一个“
未定义的错误