Consider
namespace foo
{
namespace bar
{
void f();
void f(int);
}
}
In foo
one can make all foo::bar::f
accessible as foo::f
via
using bar::f; // in foo
Is there any technical reason for the nonexistence of a syntax that makes all foo::bar::f
accessible as foo::g
like
using bar::f as g;
// or in line with using declarations for types:
using g = bar::f;
or has something like this even been considered but rejected? (Why?)
Using-declarations can be used to introduce namespace members into other namespaces and block scopes, or to introduce base class members into derived class definitions, or to introduce enumerators into namespaces, block, and class scopes (since C++20).
A using declaration in a definition of a class A allows you to introduce a name of a data member or member function from a base class of A into the scope of A .
A declarative region is a place where names can be declared in. I.e. they can be declared in a block, a class body, or in the bodies of a namespace, etc. A scope is just some snippet of program text.
See N1489:
It is possible to generalize the notion of alias beyond types and namespaces to functions, variables, etc. We do not see sufficient benefits from doing this and can imagine serious overuse leading to confusion about which functions and variables are used. Consequently, we do not propose the generalizations mentioned in this section. Furthermore, we do not plan to work further on these generalizations unless someone comes up with examples that indicate significant usefulness.
I can't think of any technical reasons for this not existing, I think it's a question of suitable (non-clashing) syntax and a significantly important use-case.
There is currently a standards proposal to add function aliases to the language in order to support opaque typedefs. According to that paper, the feature was previously considered in the early 90s, but rejected:
Stroustrup described [function aliasing] in his D&E book as a “renaming” feature in the context of resolving name clashes due to multiple inheritance: “The semantics of this concept are simple, and the implementation is trivial; the problem seems to be to find a suitable syntax.” He states that such a proposal “was presented at the standards meeting in Seattle in 1990” and that, although there was initially “a massive majority,” the feature was ultimately not adopted: “At the next meeting, . . . we agreed that such name clashes were unlikely to be common enough to warrant a separate language feature.”
The syntax proposed is as follows:
template< class RA, class R = std::less<> >
void
sort( RA b, RA const e, R lt = {} )
{
using operator<() = lt; // operator < has type R
// Remaining code in this scope uses infix < in place of calls to lt().
// (A future proposal may suggest synthesis of other relational
// operators from an operator< declared in this fashion.)
}
Perhaps someone who attended recent standards meetings could weigh in on what the reaction to the proposal was.
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