C++ Logo

liaison

Advanced search

Re: [wg14/wg21 liaison] (SC22WG14.18841) [Fwd: sudo buffer overlow]

From: Uecker, Martin <Martin.Uecker_at_[hidden]>
Date: Sat, 30 Jan 2021 15:52:29 +0000
Am Samstag, den 30.01.2021, 16:18 +0100 schrieb Arvid Norberg:
> On Sat, Jan 30, 2021 at 2:56 PM Uecker, Martin via Liaison <
> liaison_at_[hidden]> wrote:
>
> > Am Donnerstag, den 28.01.2021, 22:48 +0100 schrieb Martin Uecker:
> > .
> > >
> > > > > 3. And finally there is the problem that C++ does not
> > > > > have them.
> > > >
> > > > Once upon a time, the C++ committee spent quite some effort
> > > > on integrating VLAs.
> > > >
> > > > The history section in this paper
> > > > http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0785r0.html
> > > > is probably a good start.
> > >
> > > Thanks.
> >
> > I tried to look deeper on this, but to be honest I did not
> > fully understand why this went nowhere in C++.
> >
> > To me VLA in C seem conceptually quite simple and powerful.
> > I use them a lot in numerical code and they are extremely
> > useful. There are theoretically sound (in contrast to some
> > other language aspects related to UB) and also reasonably
> > easy to implement in compilers (I did this myself last
> > year in my toy C compiler). So why not just take the
> > C feature as is?
> >
> > It would be helpful if someone could summarize the problems
> > that make it difficult or undesirable to integrate this
> > fully into C++.
> >
>
> I wasn't around when this was discussed in the C++ committee, but I suspect
> these are some of the questions that need answers for VLAs to make it into
> C++:
>
> template <typename T>
> void v(T&& a) {
> std::cout << typeid(T).name() << '\n';
> }

Existing implementations simply make this an error.

> template <typename T, size_t N>
> void t(T (&a)[N]) {
> std::cout << N << '\n';
> }

Existing implementations do not match VLAs
against integer template arguments (which
cannot work).

> void u(std::span<int> a) {
> std::cout << a.size() << '\n';
> }

Would be nice if this could be used with VLAs.
Maybe a pointer to a VLA could be implicitly
convertable to a span<T>, but this could be
added later.

> int const len = 10;
>
> void m(int n) {
>    int a[n];   // VLA
>    int b[len]; // normal array
>
>    std::array<char, sizeof(a)> a2; // ERROR: sizeof() is a runtime value on
> VLAs

sizeof should work for VLAs as in C or an alternative
is needed (in C there is a proposal for length_of
for arrays). Of course, if this would still
not work as a template argument.

> std::array<char, sizeof(b)> b2; // OK
>
> std::array<decltype(a), 5> a3; // ERROR: decltype() on VLA

Decltype should work and seems to do so on existing
implementations. Using a VLA type as a template
type seems to get rejected. Standardizing
existing practice would be good start.

> std::array<decltype(b), 5> b3; // OK
>
> v(a); // ERROR: no matching function for call to 'v(int [n])'
> v(b); // OK
>
> t(a); // ERROR: variable-sized array type 'long int' is not a valid
> template argument
> t(b); // OK
>
> u(a); // ERROR: could not convert '(int*)(& a)' from 'int*' to
> 'std::span<int>'
> u(b); // OK
> }
>
> I think a more practical approach in C++ would be something like:
>
> template <typename T>
> std::span<T> alloca<T>(size_t n);

Nothing wrong with this, but something different that
what existing implementations already do with VLAs
and also what we have in C.


Best,
Martin

Received on 2021-01-30 09:52:36