首页 > 解决方案 > 在 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.cppI 中也替换了import std.corewith #include <iostream>,则代码会再次编译。这就像使用两个系统证明链接器来完成它的工作

问题是:我做错了吗?import同时使用新方法和旧#include方法是一种不好的做法吗?我在 Internet 上的多个帖子中看到,您可以在模块中包含一些旧标题,从而在不破坏现有代码的情况下更新您的代码。但是,如果此标头包含 STL 的某些部分,比如#include <string>我的模块使用了import std.core怎么办?

我只使用 Visual Studio 2019 进行测试,因为到目前为止,import std.core它不适用于 GCC。那么,它可能来自 VS 中的错误吗?还是所有编译器的问题都一样?

标签: c++c++20c++-modules

解决方案


是的,模块可以与头文件一起使用。我们可以在同一个文件中导入和包含头文件,这是一个例子:

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 文件的逻辑相同


推荐阅读