chr
chr

Reputation: 121

Does module names reside in a separate "name space", or could they collide with e.g. variable names?

I guess/hope that module names will not collide with e.g. names of variables. Could someone confirm this, and perhaps reference a suitable section in the (upcoming) standard?

File: a_module.cc

export module a_module;
export int add(int a, int b) { return a + b; }

// Question-1: Is using 'a_module' below as variable name allowed, or does
// the 'export module a_module' above prevent us from using that name?
int a_module = 11;

File: main.cc

import a_module;

// Question-2: Is using 'a_module' below as variable name fine, or does the
// 'import a_module' above prevent us from using that name?
int a_module = 42;

int main() { return add(1, 2); }

Upvotes: 3

Views: 158

Answers (1)

Barry
Barry

Reputation: 303287

It's hard to cite a negative, but yes - module names cannot collide with non-module names.

We have lots and lots of kinds of name lookup in C++, but none of them consider module names. Like the statement f(x); can find many kinds of things named f and x and those ()s could even mean different things, but it can never find either a module named f or x. Having modules named one or the other or both does not change any of the lookup.

Likewise, in the preamble, all module-related statements (import, export, module declarations or partition declarations) only consider module names. If you have something like:

import M1;
import M2;

And M1 happens to export some name M2, that won't break the subsequent import of M2. We're only looking for, specifically, modules named M2.

You don't have to worry about collisions between these two things.

Upvotes: 3

Related Questions