C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Named auto

From: Edward Catmur <ecatmur_at_[hidden]>
Date: Wed, 28 Sep 2022 16:08:18 +0100
On Wed, 28 Sept 2022 at 15:14, Jason McKesson via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> On Wed, Sep 28, 2022 at 10:06 AM S├ębastien Bini via Std-Proposals
> <std-proposals_at_[hidden]> wrote:
> >
> > How about a syntax similar to that of parameters then?
> >
> > template <class T> T var = foo();
> > template <class K, class V> std::pair<K const, V>& kv = *map.begin(); //
> or [k, v]?
> > template <std::copyable T, auto I> std::array<T, I> arr = getArr();
> > template <std::size_t... I> [[maybe_unused]] std::index_sequence<I...>
> seq = std::index_sequence_for<Args...>();
> >
> > Then we don't need to reinvent the syntax for parameters.
>

That syntax is already in use for variable templates. Maybe named deduction
is at block scope and variable templates are at namespace scope, but that
kind of overloading of meaning is pretty horrible - `static` is bad enough.

Let's re-examine the problem that people seem to want to solve.
>
> My understanding was that the principal problem was that you want to
> gain access to the actual typename that the placeholder deduction
> syntax yields, and sticking a `using name = decltype(var_name);` is
> both wordy and error-prone (I deliberately did that wrong, for
> example).
>
> The main reason people deduce types is because they don't want to have
> to type out the full name of the type


or because the type is unnameable, or because it's dependent, or because
the code you're calling could change its return type and so typing it out
is a hostage to fortune.


> (or because it's already
> mentioned in the initializing expression).


If you're using AAA with types on the RHS then you don't need to deduce
them again, so you wouldn't need named deduction.


> So it seems to me that
> injecting a bunch more words into such a declaration is...
> counter-productive.
>
> Yes, any of these declarations might be useful. But... are they really
> *that* useful? So useful that we want to type out (and read) all of
> that?
>

We are proposing that a single statement introduce not just potentially
multiple entity names, but also potentially multiple type aliases (also,
maybe, packs, templates, concepts). This is a novel challenge for compilers
and other tools, and it needs *some* introducing syntax to shunt the parser
down the new code paths; lexing is difficult enough as it is.

If `using` is too much typing, how about the introducer being `auto <`? It
seems that the lexer should be able to detect that pretty much immediately.

auto<class T> T var = foo();
// etc.

Received on 2022-09-28 15:08:32