简体   繁体   中英

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:

#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. 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.

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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