Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to import a function to the namespace, but not export it?

Tags:

c++

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.

like image 623
JasonN Avatar asked Oct 23 '18 23:10

JasonN


People also ask

How do I import a namespace?

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.

How do you import a namespace in C++?

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");

What do you mean by namespace?

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.


1 Answers

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 ]

like image 83
Jans Avatar answered Nov 18 '22 04:11

Jans