Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a C++ module purview?

I saw a few references to the term "purview" in the context of C++ modules, for example in https://gcc.gnu.org/wiki/cxx-modules:

Baz (); // Baz's declaration visible from purview Quux interface

What exactly is a C++ module purview?

like image 640
vitaut Avatar asked Mar 10 '18 00:03

vitaut


1 Answers

Let's break this question down into three parts:

  1. What is a C++ module?
  2. What is a purview?
  3. What is a C++ module's purview?

C++ modules

Modules are a prospective future feature of C++, and are not currently part of the language standard (i.e. not in C++17). In a nutshell, modules are intended to allow you to import parsed/compiled C++ constructs rather than textually prepending header files to your translation unit (which is what we currently do, using #include preprocessor directives). The textual inclusion of huge amounts of header text is part of what makes C++ compilation slow; if we could compile most of the "header" code once, know what it offers us and have the compiler remember just that (in the form of "modules"), we could tell the compiler we're using stuff from certain modules rather than including lots of headers.

The term "purview"

This is an English term that's used mostly in legal language, which is why a lot of us (who don't speak English natively) have never heard it. Well, let's look it up in the (Merriam-Webster) dictionary:

purview, n. :


  1. (a) the body or enacting part of a statute
    (b) the limit, purpose, or scope of a statute
  2. the range or limit of authority, competence, responsibility, concern, or intention
  3. range of vision, understanding, or cognizance

So, basically, "the purview of X" = "What X covers or contains".

The purview of C++ modules

Now let's have a look into the C++ Modules Technical Specification draft (which is what specifies the proposed modules feature). This will explain what a module's purview is:

A module unit purview starts at the module-declaration and extends to the end of the translation unit. The purview of a named module M is the set of module unit purviews of M’s module units.

When you write a module declaration, you start with declarations which aren't covered by the module (e.g. that come from elsewhere), then you "start" specifying the module's exports, then you list the declarations/definitions that are covered by the module.

Example:

// module interface of module M
int f();
export module M;
int g();
export int h();

int g() and int h() are in the "module's purview", but int f() isn't.

In this example there is just one "module unit"; if we had additional files/translation units, their relevant contents after the export module M would also be in the purview.


Note: C++20 eventually introduced a sort of a hack called the "global module fragment". Without going into details about it, suffice it to say that it appears before the export module statement. Its contents, and whatever is #included into it, is also outside the module's purview.

like image 153
einpoklum Avatar answered Oct 02 '22 17:10

einpoklum