相关疑难解决方法(0)

什么是C++模块权限?

我在C++模块的上下文中看到了一些对"purview"这个术语的引用,例如在https://gcc.gnu.org/wiki/cxx-modules中:

Baz (); // Baz's declaration visible from purview Quux interface
Run Code Online (Sandbox Code Playgroud)

究竟什么是C++模块权限?

c++ terminology c++-modules

16
推荐指数
1
解决办法
1020
查看次数

如何在 C++20 模块中使用模板显式实例化?

本答案中所述,模板实例化允许减少编译时间和大小,因为不需要为使用它们的每个新文件中的每个新类型重新编译模板。

我也很高兴C++20 模块应该如何提供一个干净的解决方案来向外部项目公开模板并减少 hpp/cpp 重复。

允许它们一起工作的语法是什么?

例如,我希望模块看起来有点像(未经测试,因此可能是错误的代码,因为我没有足够新的编译器/不确定它是否已实现):

helloworld.cpp

export module helloworld;
import <iostream>;

template<class T>
export void hello(T t) {
    std::cout << t << std::end;
}
Run Code Online (Sandbox Code Playgroud)

helloworld_impl.cpp

export module helloworld_impl;
import helloworld;

// Explicit instantiation
template class hello<int>;
Run Code Online (Sandbox Code Playgroud)

主程序

// How to prevent the full definition from being imported here, which would lead
// hello(1) to instantiate a new `hello<int>` instead of reusing the explicit instantiated
// one from `helloworld_impl.cpp`?
import helloworld;

int main() {
    hello(1); …
Run Code Online (Sandbox Code Playgroud)

c++ templates c++20 c++-modules

10
推荐指数
1
解决办法
868
查看次数

标签 统计

c++ ×2

c++-modules ×2

c++20 ×1

templates ×1

terminology ×1