C++ Logo


Advanced search

Re: [std-proposals] Allowing `using namespace` in class definition

From: Arthur O'Dwyer <arthur.j.odwyer_at_[hidden]>
Date: Mon, 13 Mar 2023 13:25:31 -0400
On Mon, Mar 13, 2023 at 1:16 PM Jason McKesson via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> On Mon, Mar 13, 2023 at 11:47 AM Andrey Semashev via Std-Proposals
> <std-proposals_at_[hidden]> wrote:
> >
> > On 3/13/23 18:14, Thiago Macieira via Std-Proposals wrote:
> > > On Monday, 13 March 2023 05:00:13 PDT Andrey Semashev via
> Std-Proposals wrote:
> > >> It would seem not fundamentally different from other
> > >> contexts, like within a function body
> > >
> > > Because they are fundamentally different.
> >
> > Could you clarify in what way?
> Declarations do very different things when they are in a class vs. not
> in a class. [...]
> `using name;` behaves differently as well. Outside of a class, that
> will... do nothing actually, since `name` already has to be accessible
> without qualification at that scope to work. By contrast, inside a
> class, it means that any base class `name`s will be available as part
> of the derived class with unqualified lookup. If `name` is not a
> member of a base class, you get a compile error.

(Nit: `using *qualified*::name;` does something useful outside a class, and
still does something different inside.)

In any case, this is one of those cases where I feel like liberal use
> of modules will basically make the problem go away. The main reason to
> want to put `using namespace` in a class is just to keep the scope
> from leaking out everywhere. With modules, they're restricted to a
> particular TU, and importing that module will not import the `using
> namespace` declaration.
> So this feature would be increasingly less useful going forward.

+1. Also, it seems like the only use-case is for UDLs, which are also
basically not-a-good-idea, an evolutionary dead end in C++11.
The motivating example can be made to compile simply by replacing "abc"sv
with "abc" (no UDL suffix).
If your program design requires using UDLs in weird places, that's just a
specific case of a design that uses UDLs at all, which is a design that
maybe shouldn't exist in this decade.

`using namespace` is also internally very weird and probably doesn't work
the way you expect it does:

my $.02,

Received on 2023-03-13 17:25:45