问题描述
我实际上是在尝试通过编写自己的小模块来理解 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),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
中我也将 import std.core
替换为 #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 文件相同