C++ Logo


Advanced search

Re: [std-proposals] Grouped-namespace "using" statements (floating the idea)

From: Jason McKesson <jmckesson_at_[hidden]>
Date: Wed, 26 Apr 2023 11:41:22 -0400
On Wed, Apr 26, 2023 at 9:21 AM Arthur O'Dwyer via Std-Proposals
<std-proposals_at_[hidden]> wrote:
> FWIW, I'm strongly opposed to this. (And I did a double-take at Barry's post: is it April Fool's Day already?) C++ isn't Perl; we don't use Unix shell globs like that. In C++, curly braces have a couple of different meanings (code block, initializer-sequence), but not "shell glob."
> (1) Ville has shown C++'s existing syntax for this:
> namespace LVT = longish::verbosish::tedious;
> using LVT::X;
> using LVT::Y;
> using LVT::Z;

This introduces an additional name that you don't need. And if people
are willing to do ridiculous stuff like putting capture-less lambdas
in `std::map` comparators just to avoid giving a name to a type,
people shouldn't have to make namespace aliases to deal with this.

> (2) It is a long-standing guideline that you shouldn't introduce multiple declarations on the same line of code.
> int x, y, z; // worse
> int x;
> int y;
> int z; // better

It should be noted that, when it comes to variable declarations, there
are a bunch of problems with shoving them on the same line
(initializers can reference each other, etc). Whereas those problems
don't exist for `using` declarations.

> (3) The "zero, one, infinity" rule. In practice, `using`-declarations rarely come in twos and threes. There's either one using-declaration (e.g. to enable ADL), or hundreds (e.g. in the implementation of the <cmath> header). Neither scenario is a good one for this proposed (mis)feature. Anytime this feature saves a significant amount of typing, the result will be unfriendly to the human reader — and the code isn't likely to have been generated by a human typist in the first place.

This practice has come about because of the primary problem with
`using` declarations: using them in headers will leak out to anyone
including those headers. So even if you could benefit from them, if
your code has to be in a header, you just can't use it. This is why
they tend to be confined to specific functions and only when
absolutely necessary.

But with modules, `using` doesn't leak. It can therefore be used for
its intended purpose: to make code more compact and readable. In an
increasingly modular world, existing practice is going to change.
We're probably going to see more `using` simply because it is
practical to do so.

So I wouldn't call it "unfriendly to the human reader." If you look at
code in other languages, you'll find that many files will list
particular components that they're including at the top of the file.

> (4) Another meaning of "C++ isn't Perl": We don't need to assign a meaning to every single sequence of ASCII characters. It's okay for some sequences of characters to be syntax errors; or to be reserved for future standardization, until we come up with a feature that is so useful that it deserves a dedicated syntax. The mere fact that "::{" is currently unassigned, is not an argument in favor of giving it this meaning.

That wasn't the point; this was solely about addressing the (implicit)
question of implementability. Because the syntax is currently an
error, it is not a breaking change to give it a meaning.

Received on 2023-04-26 15:41:35