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

  • A+
Category:Languages

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.

 


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 ]

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: