如何使用 C++ 模块:带有模板的私有片段?

dge*_*low 2 c++ templates constexpr c++20 c++-modules

我正在尝试 C++20,以更好地了解它们在实践中的工作原理。我了解了module :private可用于将接口与实现分离的片段,同时将两者保留在同一个文件中。这似乎适用于标准函数,但不适用于模板函数。

我有以下文件:

// File "main.cpp"

#include <iostream>

import mymodule;

int main()
{
    std::cout << "greeting(): " << mymodule::greetings() << std::endl;
    int x = 1;
    int y = 2;
    std::cout << "add(x, y): " << mymodule::add(x, y) << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
// File "mymodule.ixx"

module;

#include <string>

// declare interface
export module mymodule;

export namespace mymodule {
    template<typename T>
    T add(T x, T y);

    std::string greetings();
}

// implement interface
module :private;

std::string mymodule::greetings() {
    return "hello";
}

template<typename T>
T mymodule::add(T x, T y) {
    return x + y;
}
Run Code Online (Sandbox Code Playgroud)

并获得编译器警告和链接器错误(使用 Visual Studio 2022、MSVC):

Rebuild started...
1>------ Rebuild All started: Project: PlayingWithModules, Configuration: Debug x64 ------
1>Scanning sources for module dependencies...
1>mymodule.ixx
1>Compiling...
1>mymodule.ixx
1>C:\Users\Sam\Development\Cpp\Sandbox\PlayingWithModules\mymodule.ixx(29,13): warning C5226: 'mymodule::add': exported template defined in private module fragment has no reachable instantiation
1>main.cpp
1>main.obj : error LNK2019: unresolved external symbol "int __cdecl mymodule::add<int>(int,int)" (??$add@H@mymodule@@YAHHH@Z::<!mymodule>) referenced in function main
1>C:\Users\Sam\Development\Cpp\Sandbox\x64\Debug\PlayingWithModules.exe : fatal error LNK1120: 1 unresolved externals
1>Done building project "PlayingWithModules.vcxproj" -- FAILED.
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========
Run Code Online (Sandbox Code Playgroud)

我的理解是这mymodule::greetings()很好,但并不是因为编译器看不到mymodule::add(x, y)函数调用,从而导致没有生成函数。mymodule::<int, int>(x, y)<int, int>


如果我将模板函数实现为接口的一部分:

Rebuild started...
1>------ Rebuild All started: Project: PlayingWithModules, Configuration: Debug x64 ------
1>Scanning sources for module dependencies...
1>mymodule.ixx
1>Compiling...
1>mymodule.ixx
1>C:\Users\Sam\Development\Cpp\Sandbox\PlayingWithModules\mymodule.ixx(29,13): warning C5226: 'mymodule::add': exported template defined in private module fragment has no reachable instantiation
1>main.cpp
1>main.obj : error LNK2019: unresolved external symbol "int __cdecl mymodule::add<int>(int,int)" (??$add@H@mymodule@@YAHHH@Z::<!mymodule>) referenced in function main
1>C:\Users\Sam\Development\Cpp\Sandbox\x64\Debug\PlayingWithModules.exe : fatal error LNK1120: 1 unresolved externals
1>Done building project "PlayingWithModules.vcxproj" -- FAILED.
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========
Run Code Online (Sandbox Code Playgroud)

然后一切都会按预期编译并运行。


是否可以module :private与函数模板一起使用,如果可以,如何使用?或者模板函数应该始终作为接口的一部分来实现吗?我无法在线找到有关模块上下文中模板使用的详细信息,也无法找到对我收到的编译器警告的引用。

Nic*_*las 5

模块不会改变C++ 的本质,只会改变访问不同组件的方式。

C++ 本质的一部分是,为了让翻译单元使用模板,该翻译单元必须有权访问该模板的定义。模块不会改变这一点。

私有模块片段具体包含不属于模块接口的部分的内容。这就是它们的全部要点:能够将不属于界面一部分的东西粘贴到模块文件中。

因此,私有模块片段只能包含您将放入传统 .cpp 文件中的内容。事实上,这就是它们存在的原因:这样(对于短模块)您可以将所有常用的 .cpp 内容放在一个文件中,而不会影响模块生成的内容。