Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
josuttis_nm_c20_the_complete_guide.pdf
Скачиваний:
44
Добавлен:
27.03.2023
Размер:
5.85 Mб
Скачать

16.2 Modules with Multiple Files

571

Note that any attempt to use type Order in the code that imports the module results in a compile-time error. Note also that the use of the module does not depend on how many implementation units we have. The number of implementation units matters only in that the linker has to use all object files generated for them.

16.2.3 Internal Partitions

In the previous example, we introduced a data structure Order that is only used within the module. It looks like we have to declare it in the primary interface to make it available to all implementation units, which of course does not really scale in large projects.

With internal partitions, you can declare and define internal types and functions of a module in separate files. Note that partitions can also be used to define parts of an exported interface in a separate file, which we will discuss later.

Note that internal partitions are sometimes called partition implementation units, which is based on the fact that in the C++20 standard, they are officially called “module implementation units that are module partitions” and that sounds like they provide the implementations of interface partitions. They do not. They just act like internal header files for a module and may provide both declarations and definitions.

Defining an Internal Partition

Using an internal partition, we can define the local type Order in its own module unit as follows:

modules/mod2/mod2order.cppp

 

// start module unit with global module fragment

module;

 

#include <string>

 

module Mod2:Order;

// internal partition declaration

struct Order {

 

int count;

 

std::string name;

 

double price;

 

Order(int c, const std::string& n, double p) : count{c}, name{n}, price{p} {

};}

As you can see, a partition has the name of the module, then a colon, and then its partition name:

module Mod2:Order;

Sub-partitions such as Mod2:Order:Main are not supported.

You might again recognize that the file uses another new file extension: .cppp, which we will discuss later after looking at its contents.

572

Chapter 16: Modules

 

 

 

The primary interface has to import this partition by only using the name :Order:

 

 

 

modules/mod2/mod2.cppm

 

 

 

 

 

module;

// start module unit with global module fragment

 

 

 

 

 

 

 

#include <string>

 

 

 

 

 

 

#include <vector>

 

 

 

 

 

 

export module Mod2;

// module declaration

 

 

 

 

 

import :Order;

// import internal partition Order

 

 

 

 

 

export class Customer {

 

 

 

 

 

private:

 

 

 

 

 

 

std::string name;

 

 

 

 

 

 

std::vector<Order> orders;

 

 

 

 

 

public:

 

 

 

 

 

 

Customer(const std::string& n)

 

 

 

 

 

: name{n} {

 

 

 

 

 

 

}

 

 

 

 

 

 

void buy(const std::string& ordername, double price) {

 

 

 

 

 

orders.push_back(Order{1, ordername, price});

 

 

 

 

 

}

 

 

 

 

 

 

void buy(int num, const std::string& ordername, double price) {

 

 

 

 

 

orders.push_back(Order{num, ordername, price});

 

 

 

 

 

}

 

 

 

 

 

 

double sumPrice() const;

 

 

 

 

 

};double averagePrice() const;

 

 

 

 

 

 

 

The primary interface has to import the internal partition because it uses type Order. With that import, the partition is available in all units of the module. If the primary interface does not need type Order and does not import the internal partition, all module units that need type Order would have to import the internal partition directly.

Again, note that partitions are only an internal implementation aspect of a module. For the user of the code, it does not matter whether code is in the primary module, its implementation, or in an internal partition. However, code from internal partitions cannot be exported.

16.2.4 Interface Partitions

You can also split the interface of a module into multiple files. In that case, you declare interface partitions, which themselves export whatever should be exported.

}
void buy(int num, const std::string& ordername, double price) { orders.push_back(Order{num, ordername, price});
}
double sumPrice() const; double averagePrice() const; void print() const;

16.2 Modules with Multiple Files

573

Interface partitions are especially useful if modules provide multiple interfaces that are maintained by different programmers and/or teams. For simplicity, let us just use the current example to demonstrate how

to use this feature by defining only the Customer interface in a separate file.

To define only the Customer interface, we can provide the following file:

modules/mod3/mod3customer.cppm

 

 

// start module unit with global module fragment

module;

 

#include <string>

 

#include <vector>

 

export module Mod3:Customer;

// interface partition declaration

import :Order;

// import internal partition to use Order

export class Customer {

 

private:

 

std::string name;

 

std::vector<Order> orders;

 

public:

 

Customer(const std::string& n)

: name{n} {

 

}

 

void buy(const std::string& ordername, double price) {

orders.push_back(Order{1, ordername, price});

};

The partition is more or less the former primary interface with one difference:

• As a partition, we declare its name after the module name and a colon: Mod3:Customer

Like the primary interface:

We export this module partition: export module Mod3:Customer;

We use the new file extension .cppm, which we will again discuss later

574

Chapter 16: Modules

The primary interface is still the only place to specify what a module exports. However, the primary module can delegate exports to the interface partition. The way to do that is to export the imported interface partition directly as a whole:

modules/mod3/mod3.cppm

 

export module Mod3;

// module declaration

export import :Customer;

// import and export interface partition Customer

...

// import and export other interface partitions

By importing the interface partition and exporting it at the same time (yes, you have to write both keywords),

the primary interface exports the interface of the partition Customer as its own interface:

export import :Customer;

// import and export partition Customer

Importing an interface partition without exporting it is not allowed.

Again, note that partitions are only an internal implementation aspect of a module. It does not matter whether interfaces and implementations are provided in partitions. Partitions do not create a new scope.

Therefore, for the implementation of the member functions of Customer, moving the declaration of the class to a partition does not matter. You implement a member function of the class Customer as part of the module Mod3:

modules/mod3/mod3io.cpp

module;

// start module unit with global module fragment

#include <iostream>

 

#include <vector>

 

#include <format>

 

module Mod3;

// implementation unit of module Mod3

import :Order;

// import internal partition to use Order

void Customer::print() const

 

 

{

 

 

// print name:

 

 

std::cout << name << ":\n";

 

 

// print order entries:

 

 

for (const Order& od : orders) {

 

 

std::cout << std::format("{:3} {:14} {:6.2f} {:6.2f}\n",

 

od.count, od.name, od.price, od.count * od.price);

 

}

 

 

// print sum:

 

 

std::cout << std::format("{:25} ------\n", ' ');

 

 

} std::cout << std::format("{:25} {:6.2f}\n", "

Sum:", sumPrice());