Module, preamble, global module fragment and legacy header unit

65 views
Skip to first unread message

Loïc Joly

unread,
Sep 10, 2018, 6:10:10 PM9/10/18
to Evolution Working Group mailing list, mod...@isocpp.org
I've been reading the merged proposal, and I have a question.


Assume I have some code not yet fully migrated to modules, but that
makes use of modules. It can look that way:

// header1.h
#pragma once
#include "other.h"
import std.code;
void g(std::string s);


Now, if I want to use this code from a module, I have two options:

*Option 1**: Legacy header unit*

If I do:
export module mainModule;
import "header1.h"

Then a module is generated out of header1.h, 10.7.3§4 tells "Within a
legacy header unit, certain
semantic restrictions are relaxed:
— A module-import-declaration may appear, and is considered to be exported."

Does it means that this declaration may appear /anywhere/, including in
places where it would not be normally allowed?

*Option 2**: Global module*

module;
#include "header1.h"
export module mainModule;
// ...

If I do so, the preprocessed code will contain an import directive
outside of mainModule preamble, and the code is incorrect (10.7.3§1 In a
module unit, a module-import-declaration shall appear only within the
preamble.).

Do I miss something?
I not, I find this difference of behaviour between the global module
fragment and a legacy header unit disturbing.

---
Loïc


Richard Smith

unread,
Sep 10, 2018, 9:09:48 PM9/10/18
to mod...@isocpp.org, Evolution Working Group mailing list
On Mon, Sep 10, 2018 at 3:10 PM Loïc Joly <loic.act...@numericable.fr> wrote:
I've been reading the merged proposal, and I have a question.


Assume I have some code not yet fully migrated to modules, but that
makes use of modules. It can look that way:

// header1.h
#pragma once
#include "other.h"
import std.code;
void g(std::string s);


Now, if I want to use this code from a module, I have two options:

*Option 1**: Legacy header unit*

If I do:
export module mainModule;
import "header1.h"

Then a module is generated out of header1.h, 10.7.3§4 tells "Within a
legacy header unit, certain
semantic restrictions are relaxed:
— A module-import-declaration may appear, and is considered to be exported."

Does it means that this declaration may appear /anywhere/, including in
places where it would not be normally allowed?

Yes. The preamble restriction only applies to modular code, not to legacy source files.

*Option 2**: Global module*

module;
#include "header1.h"
export module mainModule;
// ...

If I do so, the preprocessed code will contain an import directive
outside of mainModule preamble, and the code is incorrect (10.7.3§1 In a
module unit, a module-import-declaration shall appear only within the
preamble.).

Do I miss something?
I not, I find this difference of behaviour between the global module
fragment and a legacy header unit disturbing.

That seems like a reasonable concern to me. I think the Modules TS permitted this, so removing that ability would be an unintended functionality regression in the merged proposal.

I'd be very interested to hear Gaby's thoughts on this.
Reply all
Reply to author
Forward
0 new messages