Meeting 16 January 2020

Modules are Coming - Bryce Adelstein Lelbach

Reddit

Modules will have a greater impact than any other feature added post-c++98.

“Ill-formed, no diagnostics required” (IFNDR)

Textual inclusion

1#include <foo.hpp>
2#include "foo.hpp"

Modular import

1import foo;
2import <foo.hpp>;
3import "foo.hpp";

Only one module declaration per translation unit:

Interface

1export module a;
2//...
3export module b; // Error

Implementation

1module a;
2//...
3module b; // Error

Module unit structure

1export module ...;
2import ...;
3...
1export declaration ...
2export {
3    declaration ...
4}
1export {
2    void f();
3    struct A;
4    int i{0};
5}
1export void f();
2export struct A;
3export int i{0};
1export template <typename T>
2T square(T t) { return t*t; }
3
4export template <typename T>
5struct is_const : false_type {};
6
7export template <typename T>
8struct is_const<T const> : true_type {};
1export namespace foo { struct A; } // foo::A exported
2
3namespace foo { struct B; } // foo::B not exported
1export typedef int int32_t;
2
3export using unsigned uint32_t;
1export import a;

  • Visible: in scope, can be named
  • Reachable: in scope, not necessarily namable

Module unit structire

1module;
2#pp-directive ...;
3export module ...;
4import ...;
5...
6module : private;
7...

  • Tools can no longer rely on simple lookup mechanism (include directories and header file names) to understand C++ projects.
  • Dependency scanning now requires a C++ parser, not just a C preprocessor.

C++ std::string_view for better performance: An example use case

Borrow types are essentially “borrowed” references to existing objects. They lack ownership; they are short-lived; they generally can do without an assignment operator. They generally appear only in function parameter lists; because they lack ownership semantics, they generally cannot be stored in data structures or returned safely from functions.

Twitter

Twitter

Quote

Ryan Campbell:

Commenting your code is like cleaning your bathroom – you never want to do it, but it really does create a more pleasant experience for you and your guests.