Is it possible to import a function (or other symbol) to a namespace but not export it? For example, I want to import std::string
into the current namespace, but I don't want current::string
to be visible.
namespace current {
using std::string;
string func();
}
current::string
should not be a thing.
The use case is simply to cut down on typing (and constantly forgetting to std::string
among others) and make the code a little more legible without all the namespace syntax cluttering the code.
To add an imported namespaceIn Solution Explorer, double-click the My Project node for the project. In the Project Designer, click the References tab. In the Imported Namespaces list, select the check box for the namespace that you wish to add. In order to be imported, the namespace must be in a referenced component.
Specify the standard namespace, for example: std::printf("example\n"); Use the C++ keyword using to import a name to the global namespace: using namespace std; printf("example\n");
A namespace is a declarative region that provides a scope to the identifiers (the names of types, functions, variables, etc) inside it. Namespaces are used to organize code into logical groups and to prevent name collisions that can occur especially when your code base includes multiple libraries.
Is it possible to import a function (or other symbol) to a namespace but not export it?
No.
A using
directive will introduce all the symbols that refer to the name you use in the declaration and they will be visible to the users of current
as if they were actually declared in the namespace itself.
The relevant part of the standard is in [namespace.udir]/1
:
A using-directive specifies that the names in the nominated namespace can be used in the scope in which the using-directive appears after the using-directive. During unqualified name lookup (6.4.1), the names appear as if they were declared in the nearest enclosing namespace which contains both the using-directive and the nominated namespace. [ Note: In this context, “contains” means “contains directly or indirectly”. — end note ]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With