C++ Logo


Advanced search

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

From: Andrey Semashev <andrey.semashev_at_[hidden]>
Date: Mon, 13 Mar 2023 21:25:11 +0300
On 3/13/23 20:25, Arthur O'Dwyer via Std-Proposals wrote:
> On Mon, Mar 13, 2023 at 1:16 PM Jason McKesson via Std-Proposals
> <std-proposals_at_[hidden] <mailto:std-proposals_at_[hidden]>>
> wrote:
> On Mon, Mar 13, 2023 at 11:47 AM Andrey Semashev via Std-Proposals
> <std-proposals_at_[hidden]
> <mailto: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.)

No, not really. In both cases, the declaration introduces a name into
the current scope, be that namespace, function scope or a class scope.
The only special case is for constructors - the using declaration
introduces a new constructor (i.e. a new name) that mimics the base
class constructor. But still, close enough.

But anyway, I was talking about using directives, not using
declarations. Extending using declarations in a similar way sounds
interesting as well, but my primary interest was in using directives.

> 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.

That UDLs are now considered a bad thing is news to me. The standard
library was extended with new UDLs as recently as in C++20[1], so it
doesn't look like this is feature is EOL.

I don't think the use case I presented is unreasonable or bad design and
therefore shouldn't exist. On the contrary, that the language doesn't
offer a way to do this looks like an omission in the language to me.

[1]: https://en.cppreference.com/w/cpp/chrono/operator%22%22d

PS: Also, as recently discussed, I don't think modules are a practical
solution any time soon.

Received on 2023-03-13 18:25:14