4

I read about modules in c++ and there's something I can't really understand how to do. I wonder how you can effectively split a c++ module into multiple files with the current merged module proposal.

Let's say I have two classes that I want to export. I want to split up my ixx files so the implementation of each of these classes stay in separated files.

I imageined something like this:

interface.ixx:

export module MyModule;

export namespace MyLib {
    struct A {
        void doStuff();
    };

    struct B {
        A myA;
        void otherStuff();
    };
}

Then, I would like to implement my classes like this,

A.ixx:

module MyModule;

// import self??

MyLib::A::doStuff() {
    // stuff...
}

B.ixx

module MyModule;

// import self again??

MyLib::B::otherStuff() {
    myA.doStuff();
}

What I want to know: Is a module, regardless the file, is aware of it's interface? If not, is there another way to split a module into mutiple files? I know it might seem silly in that case but with large classes within large module, it would be tempting to keep things separated.

Guillaume Racicot
  • 39,621
  • 9
  • 77
  • 141
  • Irrelevant, but last time I checked modules were out of C++17, did something change? – 101010 Aug 26 '16 at 08:28
  • @101010 No, modules are still out of C++17, but might be added to C++20. Sorry for my misleading tag, I will remove it. – Guillaume Racicot Aug 26 '16 at 08:30
  • 1
    Or it might be skipped again in 2020. Rather than staring into a crystal ball for the next 4 years, and looking at answers that are nothing more than fanciful wild guesses, do consider that just about any C++ compiler already supports this today. And know how to solve this problem. They just don't do it the same way. You can't get a useful answer until you name yours. – Hans Passant Aug 26 '16 at 08:46
  • This is why I mentioned "with current TS", as the definitive implementation of modules that will be added into c++ has a high chance to be similar to the current TS. – Guillaume Racicot Aug 26 '16 at 08:50

1 Answers1

2

In the merged module proposal, [module.unit]/8:

A module-declaration that contains neither export nor a module-partition implicitly imports the primary module interface unit of the module as if by a module-import-declaration.

This means that a module implementation unit implicitly imports the primary module interface of the module.

Guillaume Racicot
  • 39,621
  • 9
  • 77
  • 141