C++ Logo


Advanced search

Re: [std-proposals] Question regarding auto as template argument

From: Arthur O'Dwyer <arthur.j.odwyer_at_[hidden]>
Date: Sun, 21 May 2023 11:12:20 -0400
On Sun, May 21, 2023 at 9:52 AM Barry Revzin via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> On Sun, May 21, 2023, 3:15 AM Михаил Найденов via Std-Proposals <
> std-proposals_at_[hidden]> wrote:
>> Hello, Is the following usage of auto proposed and evaluated:
>> void func(std::vector<auto> v);
> It was valid in the Concepts TS, but it wasn't included in C++20 and
> hasn't been proposed.
> I'm not sure about the syntax though, since auto everywhere else in C++ is
> introducing a variable. Here, it's not. array<T, auto> would make more
> sense, but isn't exactly in high demand.

IMHO that's why it makes sense that it wasn't taken into C++20.
(`auto` everywhere else in C++ is, kind of, *introducing a variable*; but I
think it's also totally reasonable to think of `auto` as *replacing a type
T*, as in `auto *f()`. So allowing it to replace the type T in `vector<T>`
is defensible, if controversial. I think `vector<auto>` is vastly more
defensible than `array<T, auto>`; in the latter `auto` is neither
introducing a variable nor replacing a type.)

But as a practical note, it's worth mentioning that GCC has supported the
Concepts-TS use of `auto` even in pre-C++20 modes since 2014.

GCC 4.9 produces a warning in -std=c++11 mode, accepts silently in
-std=c++1y mode.
GCC 5, 6, 7 produce an error in -std=c++11 mode, accept silently in
-std=c++14 mode.
GCC 8, 9 produce an error in -std=c++11 mode, warn in -std=c++14 and
-std=c++17 modes, warn in -std=c++2a mode unless -fconcepts.
GCC 10+ produce an error in -std=c++11 mode, warn in -std=c++14 and
-std=c++17 modes, accept silently in -std=c++20 mode.

Pre-C++20, this was a common stumbling block in my intro training classes:
students would naturally write `auto` in the parameter list, it would
compile fine for some people and not for others, and then I'd have to
explain that that usage wasn't portable — *yet*. C++20 kind of fixed that
issue, because now the construct *is* legal C++... except for
`vector<auto>`. I'd personally prefer for GCC to just add an on-by-default
warning for it in -std=c++20 mode, rather than hope it would ever be
standardized, because I don't think it produces much benefit.


Received on 2023-05-21 15:12:34