Home > database >  make using namespace global from c module
make using namespace global from c module

Time:06-24

I am trying to expose a c namespace to whatever includes that c module. Usually in a header file I can just write using namespace x::y::z; and it'll work. I couldn't get it to work from a module.

I am using visual studio 2022 with MSVC v143, c latest.

CodePudding user response:

In the current standard draft § 10.2 [module.interface], we see:

export using namespace N;        // error: does not declare a name

In the same section, there are also correct exports of non-namespace using declarations

export using T = S;              // OK, exports name T denoting type S

and I believe that namespace aliases should also work

export namespace N = M;

The distinction is that the using namespace directive provides a tunnel for unqualified lookup to search outside its natural scope, but doesn't declare any new names. Both using declarations and namespace aliases do declare new names, and those names should be exportable.

Concretely, either of these should work:

export using float3 = linalg::aliases::float3; // for each type
export namespace la = linalg::aliases;         // or just provide a short name

CodePudding user response:

The rule here is likely to change (even in C 20 modes) via CWG2443, such that you can write

export using namespace …;

in a module with the obvious effect. (Note that a using-directive in a module interface unit already affects ordinary module units of the same module.)

Of course. this shouldn’t be considered the default way of doing things; it’s quite helpful that a using-directive can be used in a module for local readability without affecting its clients.

  • Related