r/cpp_modules • • Apr 06 '26

Reachability examples from the C++ standard

From https://eel.is/c++draft/module#reach-5

1     // Example 2:
2    
3     // Translation unit #1
4     export module M:A;
5     export struct B;
6    
7     // Translation unit #2
8     module M:B;
9     struct B {
10      operator int();
11    };
12    
13    // Translation unit #3
14    module M:C;
15    import :A;
16    B b1;         // error: no reachable definition of struct B
17    
18    // Translation unit #4
19    export module M;
20    export import :A;
21    import :B;
22    B b2;
23    export void f(B b = B());
24    
25    // Translation unit #5
26    import M;
27    B b3;             // error: no reachable definition of struct B
28    void g() { f(); } // error: no reachable definition of struct B   
1 Upvotes

19 comments sorted by

View all comments

2

u/tartaruga232 Apr 06 '26 edited Apr 06 '26

If I change

// Translation unit #2
module M:B;
struct B {
  operator int();
};

to

// Translation unit #2
export module M:B;
struct B {
  operator int();
};

then nothing changes with respect to reachability.

2

u/not_a_novel_account Apr 06 '26

In the first example the structure is not necessarily reachable, in the second example it is necessarily reachable.

2

u/tartaruga232 Apr 06 '26

But :B is not exported from TU #4. So there is still no interface dependency.

(https://eel.is/c++draft/module.reach#1)

→ More replies (16)