1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
Modules and namespaces are mutually independent. Modules (and/or partitions,
cf. section ref(PARTITIONS))
can define (contain) namespaces and/or they can define components in
namespaces. Source files importing such modules must either explicitly specify
the namespace of such components or they can specify a tt(using namespace)
declarations avoiding the explicit namespace specification when referring to
components living in those namespaces.
Here is a simple example of a module exporting a variable which is defined in
a namespace:
verbinclude(-as4 examples/namespace/ns/modns.cc)
It's a very basic example, illustrating the essence of defining a namespace
completely covering the module's export compound. Variations are possible: a
namespace could completely surround the export compund, a namespace could be
defined inside the export compound, only some of the module's components could
belong to a namespace, non-exported components could belong to a namespace,
multiple namespaces could be used, etc., etc..
To use the variable tt(g_count), its module is imported and its namespace is
specified as usual. E.g.,
verbinclude(-as4 examples/namespace/main.cc)
Alternatively the source file could specify some tt(using namespace)
declarations so the repeated namespace specifications can be avoided:
verbinclude(-as4 examples/namespace/main.2)
Some notes:
itemization(
it() Since module interfaces em(must) start with tt(export module) modules
cannot be defined inside namespaces. Constructions like
verb( namespace Area
{
export module Nested;
... declarations of components of Nested
}
)
won't compile. But as shown, defining a namespace section inside a
module is fine.
it() Modules cannot be imported inside a namespace area as tt(import)
declarations cannot be nested (i.e., they must be at global scope) So
constructions like the following also won't compile:
verb( namespace FBB
{
import NS;
}
)
)
|