简体   繁体   English

如何在 C++20 中参数化模块?

[英]How to parametrize modules in C++20?

I mean a situation like this:我的意思是这样的情况:

#define __STDC_FORMAT_MACROS 1
#include <inttypes.h>

This exactly example is maybe of rare use, but it's just an example, and this method is used quite widely, especially inside projects.这个例子可能很少用到,但它只是一个例子,这种方法使用得相当广泛,尤其是在项目内部。 Macros defined for system or compiler type and version are taken up similar way, also macros defined in the compiler command line.为系统或编译器类型和版本定义的宏采用类似的方式,在编译器命令行中定义的宏也是如此。

My question is whether there exists some mechanism to be used by the developer that is about to use the import declaration, when you already have a module using a normal name, something like this:我的问题是,当您已经有一个使用普通名称的模块时,是否存在某种机制可供即将使用import声明的开发人员使用,如下所示:

#define __STDC_FORMAT_MACROS 1
#export __STDC_FORMAT_MACROS
import std.inttypes;

Or, maybe even better - specify the parameters exported TO the module exclusively for it (that wouldn't spread to the others).或者,也许更好——指定导出到模块的参数专用于它(不会传播到其他模块)。 Is there any mechanism that allows it to be achieved?是否有任何机制可以实现它?

About 80% of the entire point of modules is that a module is a fixed object, with its definitions being entirely unaffected by anything outside of the module itself.大约 80% 的整个模块点是模块是固定的 object,其定义完全不受模块本身之外的任何影响。 If you could "parameterize" them, that'd make them kind of worthless, since every time you import a module, you'd have to recompile it.如果您可以“参数化”它们,那会使它们变得毫无价值,因为每次导入模块时,都必须重新编译它。 Which is exactly the problem modules exist to prevent.这正是存在要防止的问题模块。

The only parameters modules take are command line arguments to the compiler, just like any other translation unit.模块采用的唯一参数是编译器的命令行 arguments,就像任何其他翻译单元一样。

If you're the one writing the module, you can use templates and specialization to configure/parameterize it.如果您是模块的编写者,则可以使用模板和专业化来配置/参数化它。 Consider the following:考虑以下:

//=====================================
// Module
//=====================================
template<typename EnableIf = void>
struct FooConfig {
    static constexpr int value = -1;
};

template<typename Config = FooConfig<> >
void foo() {
  // Some functionality based on the configuration settings.
  std::cout << Config::value << std::endl;
}

//=====================================
// Module configuration
// (lives outside module)
//=====================================
template<>
struct FooConfig<void> {
  static constexpr int value = 2;
};

https://godbolt.org/z/df5185cP4 https://godbolt.org/z/df5185cP4

Unlike with macros, the "parameterization" happens after import.与宏不同,“参数化”发生导入之后。 The module itself is fixed - though you can specialize things within it, including other template functions to enable whatever custom behavior you want.模块本身是固定的——尽管你可以在其中专门化一些东西,包括其他模板函数来启用你想要的任何自定义行为。

So, following the discussion in one of the other answers, you could put in all the implementations for different encryption libraries (assuming they are all available on the system building the module) and use a configuration parameter to select between them.因此,按照其他答案之一的讨论,您可以放入不同加密库的所有实现(假设它们在构建模块的系统上都可用)并在它们之间使用 select 的配置参数。

Or probably more practically, you would have your base library provide an interface for encryption (with a default of none), then have a separate module provide the implementation, and use either a configuration parameter or other template specialization mechanisms to pick up that implementation.或者更实际地,您可以让您的基础库提供一个加密接口(默认为无),然后让一个单独的模块提供实现,并使用配置参数或其他模板专门化机制来获取该实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM