Home > Back-end >  If a C module partition B imports module partition A, is anything imported by partition A also vis
If a C module partition B imports module partition A, is anything imported by partition A also vis

Time:12-02

In one module where I have partitions, I noticed that if a partition imports another partition, everything the second partition imports is also visible in the first partition. Is this correct behavior or a bug in the compiler?

I am using VS2022.

Lets say we have some module Foo:

// Foo.ixx

export module Foo;

export void foo()
{
};

And we have another module Bar with two partitions:

// Bar.ixx

export module Bar;

export import :PartA;
export import :PartB;

The first partition imports module Foo:

// PartA.ixx

export module Bar:PartA;
import Foo;

export void partA()
{
    foo(); 
}

The second partition imports the first partition:

// Part B.ixx

export module Bar:PartB;
import :PartA;

export void partB()
{
    partA();
    foo();  // should this compile?
}

Partition partB is calling function foo() from module Foo. partB did not import Foo, but partA did. In VS2022, the project compiles fine but Intellisense complains that 'foo' is undefined. Which one is correct?

CodePudding user response:

[basic.scope.namespace]/2 spells out whether a name used in one TU is in scope of a TU that imports it. The short version is that foo is visible if Bar:Part2 imports Foo.

So... does it?

Yes.

When a module partition unit imports another partition (which must be of the same module, since you cannot import someone else's partitions), it also implicitly imports non-exported TUs import by that partition:

Additionally, when a module-import-declaration in a module unit of some module M imports another module unit U of M, it also imports all translation units imported by non-exported module-import-declarations in the module unit purview of U.

So Bar:Part2 indirectly imports Foo.

  • Related